<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mysql &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Thu, 16 Nov 2023 09:37:45 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.2</generator>
	<item>
		<title>php5.4升php7.3踩坑之路</title>
		<link>https://www.mudbest.com/php5-4%e5%8d%87php7-3%e8%b8%a9%e5%9d%91%e4%b9%8b%e8%b7%af/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Fri, 12 Mar 2021 18:24:51 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[mysql5.7]]></category>
		<category><![CDATA[php5升级php7]]></category>
		<category><![CDATA[php7]]></category>
		<guid isPermaLink="false">https://www.mudbest.com/?p=2200</guid>

					<description><![CDATA[6年没有更新博客了，近期计划内项目有一些web服务底层基于php7+、mysql5.7+开发的，而服务器的ph ... <a title="php5.4升php7.3踩坑之路" class="read-more" href="https://www.mudbest.com/php5-4%e5%8d%87php7-3%e8%b8%a9%e5%9d%91%e4%b9%8b%e8%b7%af/" aria-label="阅读 php5.4升php7.3踩坑之路">阅读更多</a>]]></description>
										<content:encoded><![CDATA[
<blockquote class="wp-block-quote is-style-default is-layout-flow wp-block-quote-is-layout-flow">
<p>6年没有更新博客了，近期计划内项目有一些web服务底层基于php7+、mysql5.7+开发的，而服务器的php版本停留在了5.4一直未更新(mysql.5.5)，导致环境无法部署，考虑到运行的服务比较关键致使升级的计划一而再被拖延。</p>
</blockquote>

<p>&nbsp;</p>
<p>&nbsp;</p>

<p class="wp-block-paragraph">这次决定将php5.4和mysql5.5升级至php7.3和mysq5.7做个记录，希望能帮助到其他类似需要升级的人。</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>先说一下版本情况：</p>
<cite>centos6.8，php5.4，mysql5.7</cite></blockquote>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph"><strong>升级php7.3过程比较简单。</strong></p>



<p class="wp-block-paragraph">1、安装epel源</p>


<pre><pre class="brush: bash; title: ; notranslate">
yum install epel-release
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">2、加载remirepo的php源(注意：centos7不是用这个源)</p>


<pre><pre class="brush: bash; title: ; notranslate">
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">2、安装php7.3</p>


<pre><pre class="brush: bash; title: ; notranslate">
yum --enablerepo=remi-php73 install php
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">3、安装主要扩展包</p>


<pre><pre class="brush: bash; title: ; notranslate">
yum --enablerepo=remi-php73 install php-xml php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt php-fpm
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">4、安装完成后，重启php-fpm</p>


<pre><pre class="brush: bash; title: ; notranslate">
kill -USR2 `cat /var/run/php-fpm/php-fpm.pid`
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">5、以下非必要</p>


<pre><pre class="brush: bash; title: ; notranslate">
yum update
</pre>



<p class="wp-block-paragraph">至此，php7.3安装完成，此处我遇到1个关于mysqli扩展的问题，默认php-fpm的配置是加载了mysqli的，但是不知道为什么我查看phpinfo是没有mysqli扩展信息的，后来在php.ini里开启了mysqli扩展才识别了，不知道其他人有没有遇到。</p>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph"><strong>接着就是升级mysql5.4至mysql5.7了。</strong></p>



<p class="wp-block-paragraph">首先备份数据库，升级MySQL通常不会丢失数据，但保险起见，我们需要做这一步。</p>



<p class="wp-block-paragraph">这种方式备份的侧重点是库与表，备份文件里面会有use db_name这样的语句，方便于恢复数据时知道要把表创建到哪个数据库。</p>


<pre><pre class="brush: bash; title: ; notranslate">
mysqldump -uroot -p &#x5B;--no-data] --databases db_name1 db_name2 &amp;amp;amp;amp;gt; /dir/backup.sql;  //--no-data指不备份数据，只备份库与表结构。
</pre>


<pre><pre class="brush: bash; title: ; notranslate">
mysqldump -u xxx -h xxx -P 3306 -p --all-databases &amp;amp;amp;amp;gt; databases.sql;   // 备份所有库
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">1、备份完数据库之后，开始卸载旧版本 MySQL</p>


<pre><pre class="brush: bash; title: ; notranslate">
yum remove mysql mysql-*
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">2、确认mysql是否卸载完全</p>


<pre><pre class="brush: bash; title: ; notranslate">
yum list installed | grep mysql
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">3、上面的情况是没有未删除部分，如果有的话可通过下面的命令删除：</p>


<pre><pre class="brush: bash; title: ; notranslate">
yum remove mysql－libs
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">4、查看系统版本</p>


<pre><pre class="brush: bash; title: ; notranslate">
&#x5B;root@iZbp18bpfv23id1yoiv3f0Z include]# uname -r
2.6.32-754.35.1.el6.x86_64
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">5、mysql官方源太慢了，这里用第三方源(<a href="http://mirrors.sohu.com/mysql/MySQL-5.7/" target="_blank" rel="noreferrer noopener" data-type="URL" data-id="http://mirrors.sohu.com/mysql/MySQL-5.7/">传送门</a>)</p>



<p class="wp-block-paragraph">//贴图</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="824" height="635" class="wp-image-2203" src="https://www.mudbest.com/wp-content/uploads/2021/03/mysql-1.png" alt="" srcset="https://www.mudbest.com/wp-content/uploads/2021/03/mysql-1.png 824w, https://www.mudbest.com/wp-content/uploads/2021/03/mysql-1-558x430.png 558w, https://www.mudbest.com/wp-content/uploads/2021/03/mysql-1-768x592.png 768w" sizes="(max-width: 824px) 100vw, 824px" /></figure>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">6、然后根据自己系统配置选择对应的 rpm 包(我这里下载的是5.7.23)，鼠标右键打开复制链接，然后到软件包目录执行下载命令</p>


<pre><pre class="brush: bash; title: ; notranslate">
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.23-1.el6.x86_64.rpm-bundle.tar
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">7、解压包</p>


<pre><pre class="brush: bash; title: ; notranslate">
tar -zcvf mysql-5.7.23-1.el6.x86_64.rpm-bundle.tar
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">8、安装rpm以及相关依赖</p>


<pre><pre class="brush: bash; title: ; notranslate">
yum localinstall mysql-community-common-5.7.23-1.el6.x86_64.rpm
</pre>


<pre><pre class="brush: bash; title: ; notranslate">
yum localinstall mysql-community-libs-5.7.23-1.el6.x86_64.rpm
</pre>


<pre><pre class="brush: bash; title: ; notranslate">
yum localinstall mysql-community-client-5.7.23-1.el6.x86_64.rpm
</pre>


<pre><pre class="brush: bash; title: ; notranslate">
yum localinstall mysql-community-server-5.7.23-1.el6.x86_64.rpm
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">9、OK安装完成，查看版本</p>


<pre><pre class="brush: bash; title: ; notranslate">
&#x5B;root@*** include]# mysql -V
mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">10、重启mysql</p>


<pre><pre class="brush: bash; title: ; notranslate">
service mysqld start
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">11、如果重启mysql失败，初始化数据库。</p>


<pre><pre class="brush: bash; title: ; notranslate">
mysqld --initialize
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">12、基于mysql5.7的安全机制有变更，默认mysql是有密码的，查看默认密码。</p>


<pre><pre class="brush: bash; title: ; notranslate">
&#x5B;root@******************* include]# grep 'temporary password' /var/log/mysqld.log
2021-03-09T10:52:05.394381Z 1 &#x5B;Note] A temporary password is generated for root@localhost: *****?4#v
2021-03-09T10:54:04.766642Z 1 &#x5B;Note] A temporary password is generated for root@localhost: *****zyc6/(
2021-03-09T10:55:15.770123Z 1 &#x5B;Note] A temporary password is generated for root@localhost: *****Vy!y0!h
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">13、还是基于安全，默认登录到mysql命令控制台，执行任何有效命令都会提示重新设置密码。</p>


<pre><pre class="brush: bash; title: ; notranslate">
mysql -&amp;amp;amp;amp;amp;gt; select host from mysql.user where User='root';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">14、修改密码(5.7的密码验证要求高，密码过于简单会提示错误)</p>


<pre><pre class="brush: bash; title: ; notranslate">
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">15、密码过于简单报错</p>


<pre><pre class="brush: bash; title: ; notranslate">
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">16、最后让修改生效执行</p>


<pre><pre class="brush: bash; title: ; notranslate">
flush privileges;
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">至此，mysql的安装以及配置完成，剩下就是进入控制台 source导入上面备份的数据库就行了。</p>



<p class="wp-block-paragraph">==================================================我是分割线</p>



<p class="wp-block-paragraph">踩坑开始：</p>



<p class="wp-block-paragraph">由于论坛(<a href="https://bbs.mudbest.com" target="_blank" rel="noreferrer noopener">https://bbs.mudbest.com</a>)是基于discuz7.2开发的，很多函数在php7.3被废弃了甚至会报错，php7.3和mysql5.7升级完成之后，然后一脸懵逼。。论坛挂了。。。无法访问。</p>



<p class="wp-block-paragraph">现在回过头看discuz7的代码(无法言喻。。)，打开php-fpm的运行错误，在php-fpm.conf内添加以下内容，然后打开error_log就可以看到运行的错误信息了</p>


<pre><pre class="brush: bash; title: ; notranslate">
&#x5B;www]
catch_workers_output = yes
</pre>



<p class="wp-block-paragraph">坑整理：<br />1、set_magic_quotes_runtime 报致命错误。<br />2、ob_start('ob_gzhandler') 报致命错误。<br />3、preg_replace 带evel的 报致命错误。<br />4、php7 session不能传递的解决方案<br />5、….各种告警(修了70%，剩下30%不想修了，直接改告警登记屏蔽了，哈哈)</p>



<p class="wp-block-paragraph"><strong>关于问题3、4 的解决方式：</strong><br />1、针对 preg_replace evel替代方案就是将 全站用到 evel的换成preg_replace_callback，来个例子。</p>


<pre><pre class="brush: php; title: ; notranslate">
$message = preg_replace(&quot;/\s?&#x5B;code](.+?)&#x5B;\/code]\s?/ies&quot;, &quot;codedisp('\1')&quot;, $message);
</pre>



<p class="wp-block-paragraph">改写成</p>


<pre><pre class="brush: php; title: ; notranslate">
$message = preg_replace_callback(
&quot;/\s?&#x5B;code](.+?)&#x5B;\/code]\s?/is&quot;,
function ($matches) {
return codedisp($matches&#x5B;1]);
},
$message
);
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph"><em>扩展知识：</em></p>


<pre><pre class="brush: php; title: ; notranslate">
//适合单一任务
$template = preg_replace_callback( //处理eval
    &quot;/&#x5B;\n\r\t]*\{eval\s+(.+?)\}&#x5B;\n\r\t]*/is&quot;,
    function ($matches) { return str_replace(&quot;\\\&quot;&quot;, &quot;\&quot;&quot;, preg_replace(&quot;/\&amp;amp;amp;amp;amp;lt;\?\=(\\\$.+?)\?\&amp;amp;amp;amp;amp;gt;/s&quot;, &quot;\\1&quot;, &quot;&amp;amp;amp;amp;amp;lt;? $matches&#x5B;1] ?&amp;amp;amp;amp;amp;gt;&quot;)); },
    $template
);
 
//需要同一正则处理多个任务时  &#x5B;推荐]
function stripvtags($expr) {
    $expr = str_replace(&quot;\\\&quot;&quot;, &quot;\&quot;&quot;, preg_replace(&quot;/\&amp;amp;amp;amp;amp;lt;\?\=(\\\$.+?)\?\&amp;amp;amp;amp;amp;gt;/s&quot;, &quot;\\1&quot;, $expr));
    return $expr;
}
$template = preg_replace_callback( //处理eval
    &quot;/&#x5B;\n\r\t]*\{eval\s+(.+?)\}&#x5B;\n\r\t]*/is&quot;,
    function ($matches) { return stripvtags(&quot;&amp;amp;amp;amp;amp;lt;? $matches&#x5B;1] ?&amp;amp;amp;amp;amp;gt;&quot;); },
    $template
);
$template = preg_replace_callback( //处理echo
    &quot;/&#x5B;\n\r\t]*\{echo\s+(.+?)\}&#x5B;\n\r\t]*/is&quot;,
    function ($matches) { return stripvtags(&quot;&amp;amp;amp;amp;amp;lt;? echo $matches&#x5B;1] ?&amp;amp;amp;amp;amp;gt;&quot;); },
    $template
);
 
//与第一种差不多, 只不过是分开写
class Template {
    public function test() {
        $template = preg_replace_callback( //处理eval
            &quot;/&#x5B;\n\r\t]*\{eval\s+(.+?)\}&#x5B;\n\r\t]*/is&quot;,
            array($this, 'stripvtagsEval'),
            $template
        );
    }
    protected function stripvtagsEval($matches) {
        return str_replace(&quot;\\\&quot;&quot;, &quot;\&quot;&quot;, preg_replace(&quot;/\&amp;amp;amp;amp;amp;lt;\?\=(\\\$.+?)\?\&amp;amp;amp;amp;amp;gt;/s&quot;, &quot;\\1&quot;, &quot;&amp;amp;amp;amp;amp;lt;? $matches&#x5B;1] ?&amp;amp;amp;amp;amp;gt;&quot;)); 
    }
}
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">2、session问题只需要将php.ini内的session.use_trans_sid设置为1，并且将存放session的目录权限改为可写入。</p>


<pre><pre class="brush: bash; title: ; notranslate">
session.use_trans_sid=0
</pre>



<p class="wp-block-paragraph">&nbsp;</p>



<p class="wp-block-paragraph">改为</p>


<pre><pre class="brush: bash; title: ; notranslate">
session.use_trans_sid=1
</pre>

<p>&nbsp;</p>

<p class="wp-block-paragraph">&nbsp;</p>

<p>验证码无法显示的原因，可以看查看一下session.save_path目录的权限，默认这个目录是给apache，如果nginx启动用户非apache则无法写入session 默认session.save_page目录为/var/lib/php</p>
<pre><pre class="brush: bash; title: ; notranslate">
drwxrwx--- 2 root apache   6 Aug  4 02:40 opcache
drwxr-xr-x 2 root root   116 Nov 15 16:43 peclxml
drwxrwx--- 2 root apache   6 Aug  4 02:40 session
drwxrwx--- 2 root apache   6 Aug  4 02:40 wsdlcache
</pre></pre>
<p>我这里nginx改了启动用户，将www改成自己配置的用户组即可。20231116</p>
<pre><pre class="brush: bash; title: ; notranslate">
chown www:www /var/lib/php -R
</pre></pre>

<p class="wp-block-paragraph">至此，从升级配置直至填坑恢复，用了2天，又掉了不少头发，赶紧洗洗睡了。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>VMware下Centos的lnmp编译安装</title>
		<link>https://www.mudbest.com/vmware%e4%b8%8bcentos%e7%9a%84lnmp%e7%bc%96%e8%be%91%e5%ae%89%e8%a3%85/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Tue, 29 Apr 2014 04:36:07 +0000</pubDate>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[lnmp]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=2043</guid>

					<description><![CDATA[VMware安装Centos就不说了，接下来是Centos的相关配置，如果Centos有防火墙的话，则需要配置 ... <a title="VMware下Centos的lnmp编译安装" class="read-more" href="https://www.mudbest.com/vmware%e4%b8%8bcentos%e7%9a%84lnmp%e7%bc%96%e8%be%91%e5%ae%89%e8%a3%85/" aria-label="阅读 VMware下Centos的lnmp编译安装">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>VMware安装Centos就不说了，接下来是Centos的相关配置，如果Centos有防火墙的话，则需要配置防火墙，非Vm通用。</p>
<p><strong>一、开启80端口、3306端口</strong><span id="more-2043"></span></p>
<pre class="brush: bash; title: ; notranslate">
vi /etc/sysconfig/iptables   #编辑防火墙配置文件 
</pre>
<p>-A INPUT -m state &#8211;state NEW -m tcp -p tcp &#8211;dport 80 -j ACCEPT（允许80端口通过防火墙）<br />
-A INPUT -m state &#8211;state NEW -m tcp -p tcp &#8211;dport 3306 -j ACCEPT（允许3306端口通过防火墙）   </p>
<pre class="brush: bash; title: ; notranslate"> 
#特别提示：很多网友把这两条规则添加到防火墙配置的最后一行，导致防火墙启动失败
#正确的应该是添加到默认的22端口这条规则的下面，添加好之后防火墙规则如下所示：
#########################################################    
# Firewall configuration written by system-config-firewall    
# Manual customization of this file is not recommended.    
*filter    
:INPUT ACCEPT &#x5B;0:0]    
:FORWARD ACCEPT &#x5B;0:0]    
:OUTPUT ACCEPT &#x5B;0:0]    
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT    
-A INPUT -p icmp -j ACCEPT    
-A INPUT -i lo -j ACCEPT    
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT    
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT    
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT    
-A INPUT -j REJECT --reject-with icmp-host-prohibited    
-A FORWARD -j REJECT --reject-with icmp-host-prohibited    
COMMIT    
#########################################################    
/etc/init.d/iptables restart  #最后重启防火墙使配置生效 
</pre>
<p><strong>二、关闭SELINUX</strong></p>
<pre class="brush: bash; title: ; notranslate">
vim /etc/selinux/config  #编辑
#SELINUX=enforcing       #注释掉 
#SELINUXTYPE=targeted    #注释掉 
SELINUX=disabled         #增加 
:wq #保存退出
shutdown -r now   #重启系统
</pre>
<p><strong>三、系统约定</strong><br />
软件源代码包存放位置：/usr/local/src<br />
源码包编译安装位置：/usr/local/软件包名字</p>
<p><strong>四、源码包下载</strong><br />
1、pcre下载(支持nginx伪静态)<br />
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz</p>
<p>2、mysql下载<br />
http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.17.tar.gz</p>
<p>3、libmcrypt-2.5.7<br />
ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz</p>
<p>3.2、libiconv-1.9.2<br />
http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.9.2.tar.gz</p>
<p>3.3、zlib下载<br />
http://zlib.net/zlib-1.2.8.tar.gz</p>
<p>3.4、php下载<br />
http://ar2.php.net/distributions/php-5.5.11.tar.gz<br />
http://ar2.php.net/distributions/php-5.3.28.tar.gz</p>
<p>3.5、phpredis扩展下载<br />
https://github.com/nicolasff/phpredis/archive/master.zip</p>
<p>4、cmake下载(MySQL编译工具)<br />
http://www.cmake.org/files/v2.8/cmake-2.8.8.tar.gz</p>
<p>5、nginx下载(稳定版本)<br />
http://nginx.org/download/nginx-1.4.7.tar.gz</p>
<p>安装编译工具及库文件（使用CentOS yum命令安装）</p>
<pre class="brush: bash; title: ; notranslate">
yum install make apr* autoconf automake curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd  kernel keyutils  patch  perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch freetype-devel
</pre>
<p><strong>五、开始编译安装</strong><br />
1、编译准备工作</p>
<pre class="brush: bash; title: ; notranslate">
yum groupinstall &quot;Development tools&quot;
</pre>
<p>1.1、安装cmake</p>
<pre class="brush: bash; title: ; notranslate">
cd /usr/local/src 
tar zxvf cmake-2.8.8.tar.gz 
cd cmake-2.8.8 
./configure make  #编译 
make install   #安装
</pre>
<p>2、安装MySQL</p>
<pre class="brush: bash; title: ; notranslate">
groupadd mysql  #添加mysql组    
useradd -g mysql mysql -s /bin/false  #创建用户mysql并加入到mysql组，不允许mysql用户直接登录系统    
mkdir -p /data/mysql  #创建MySQL数据库存放目录    
chown -R mysql:mysql /data/mysql   #设置MySQL数据库目录权限    
mkdir -p /usr/local/mysql #创建MySQL安装目录    
cd /usr/local/src    
tar zxvf mysql-5.5.25.tar.gz  #解压    
cd mysql-5.5.25    
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ -DMYSQL_DATADIR=/data/mysql/ -DSYSCONFDIR=/etc/  #配置    
make #编译    
make install  #安装    
cd /usr/local/mysql    
cp ./support-files/my-huge.cnf  /etc/my.cnf   #拷贝配置文件（注意：如果/etc目录下面默认有一个my.cnf，直接覆盖即可）    
vim /etc/my.cnf   #编辑配置文件,在 &#x5B;mysqld] 部分增加下面一行    
datadir = /data/mysql  #添加MySQL数据库路径    
:wq!  #保存退出    
cd /usr/local/mysql/ #share 初始化的sql在这里面,下面一句命令默认读取share路径是./share/...
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/ &amp; #生成mysql系统数据库,并定义数据data存放位置
cp ./support-files/mysql.server  /etc/rc.d/init.d/mysqld  #把Mysql加入系统启动    
chmod 755 /etc/init.d/mysqld   #增加执行权限    
chkconfig mysqld on  #设置开机启动    
vim /etc/rc.d/init.d/mysqld  #编辑    
basedir = /usr/local/mysql   #MySQL程序安装路径    
datadir = /data/mysql  #MySQl数据库存放目录    
service mysqld start  #启动    
vim /etc/profile   #把mysql服务加入系统环境变量：在最后添加下面这一行    
export PATH=$PATH:/usr/local/mysql/bin    
:wq! #保存退出    
</pre>
<p>下面这两行把myslq的库文件链接到系统默认的位置，在编译类似PHP等软件时可以不用指定mysql的库文件地址。 </p>
<pre class="brush: bash; title: ; notranslate">
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock #创建快捷方式

ln -s /usr/local/mysql/lib /usr/lib/mysql    
ln -s /usr/local/mysql/include/mysql /usr/include/mysql    
shutdown -r now     #需要重启系统，等待系统重新启动之后继续在终端命令行下面操作    
mysql_secure_installation    #设置Mysql密码    

mysql --socket=/var/lib/mysql/mysql.sock #指定sock链接


</pre>
<p>根据提示按Y 回车（默认密码为空）<br />
然后输入2次密码<br />
继续按Y 回车，直到设置完成<br />
或者直接修改密码</p>
<pre class="brush: bash; title: ; notranslate">
/usr/local/mysql/bin/mysqladmin -u root -p password &quot;123456&quot; #修改密码    
service mysqld restart  #重启    
</pre>
<p>到此，mysql安装完成。</p>
<p>3、安装pcre</p>
<pre class="brush: bash; title: ; notranslate">
cd /usr/local/src    
mkdir /usr/local/pcre  #创建安装目录    
tar  zxvf pcre-8.30.tar.gz    
cd pcre-8.30    
./configure  --prefix=/usr/local/pcre  #配置    
make    
make install
</pre>
<p>4、安装nginx</p>
<pre class="brush: bash; title: ; notranslate">
cd /usr/local/src    
groupadd  www  #添加www组    
useradd -g  www www -s /bin/false  #创建nginx运行账户www并加入到www组，不允许www用户直接登录系统    
tar  zxvf nginx-1.2.0.tar.gz    
cd nginx-1.2.0    
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/usr/local/src/pcre-8.30    
#注意:--with-pcre=/usr/local/src/pcre-8.30指向的是源码包解压的路径，而不是安装的路径，否则会报错    
make    
make install    
/usr/local/nginx/sbin/nginx   #启动nginx    
</pre>
<pre class="brush: bash; title: ; notranslate">
vim /etc/rc.d/init.d/nginx    #设置nginx开启启动，编辑启动文件添加下面内容 
</pre>
<pre class="brush: bash; title: ; notranslate">
#################################################################    
#!/bin/bash    
# nginx Startup script for the Nginx HTTP Server    
# it is v.0.0.2 version.    
# chkconfig: - 85 15    
# description: Nginx is a high-performance web and proxy server.    
#              It has a lot of features, but it's not for everyone.    
# processname: nginx    
# pidfile: /var/run/nginx.pid    
# config: /usr/local/nginx/conf/nginx.conf    
nginxd=/usr/local/nginx/sbin/nginx    
nginx_config=/usr/local/nginx/conf/nginx.conf    
nginx_pid=/usr/local/nginx/logs/nginx.pid    
RETVAL=0    
prog=&quot;nginx&quot;    
# Source function library.    
.  /etc/rc.d/init.d/functions    
# Source networking configuration.    
.  /etc/sysconfig/network    
# Check that networking is up.    
&#x5B; ${NETWORKING} = &quot;no&quot; ]  exit 0    
&#x5B; -x $nginxd ] || exit 0    
# Start nginx daemons functions.    
start() {    
if &#x5B; -e $nginx_pid ];then    
  echo &quot;nginx already running....&quot;    
  exit 1    
fi    
  echo -n $&quot;Starting $prog: &quot;    
  daemon $nginxd -c ${nginx_config}    
  RETVAL=$?    
  echo    
  &#x5B; $RETVAL = 0 ]  touch /var/lock/subsys/nginx    
  return $RETVAL    
}    
# Stop nginx daemons functions.    
stop() {    
       echo -n $&quot;Stopping $prog: &quot;    
       killproc $nginxd    
       RETVAL=$?    
       echo    
       &#x5B; $RETVAL = 0 ]  rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid    
}    
reload() {    
   echo -n $&quot;Reloading $prog: &quot;    
   #kill -HUP `cat ${nginx_pid}`    
   killproc $nginxd -HUP    
   RETVAL=$?    
   echo    
}    
# See how we were called.    
case &quot;$1&quot; in    
start)    
       start    
       ;;    
stop)    
       stop    
       ;;    
reload)    
       reload    
       ;;    
restart)    
       stop    
       start    
       ;;    
               
status)    
       status $prog    
       RETVAL=$?    
       ;;    
*)    
       echo $&quot;Usage: $prog {start|stop|restart|reload|status|help}&quot;    
       exit 1    
esac    
exit $RETVAL    
#################################################################  
:wq!  #保存退出 
</pre>
<pre class="brush: bash; title: ; notranslate">   
chmod 775  /etc/rc.d/init.d/nginx  #赋予文件执行权限    
chkconfig nginx on    #设置开机启动    
/etc/rc.d/init.d/nginx restart  #重启    
</pre>
<p>5.1、安装libmcrypt</p>
<pre class="brush: bash; title: ; notranslate">
cd /usr/local/src    
tar zxvf  libmcrypt-2.5.7.tar.gz   #解压    
cd  libmcrypt-2.5.7 #进入目录    
./configure    #配置    
make             #编译    
make install   #安装  
</pre>
<p>5.2、安装libiconv</p>
<pre class="brush: bash; title: ; notranslate">
cd /usr/local/src    
tar -zxvf libiconv-1.9.2.tar.gz   #解压    
cd  libiconv-1.9.2 #进入目录    
./configure --prefix=/usr/local/iconv    #配置    
make             #编译    
make install   #安装  

#重新编译php 增加编译参数--with-iconv=/usr/local/iconv 
</pre>
<p>5.3、安装zlib</p>
<pre class="brush: bash; title: ; notranslate">
yum info zlib
cd /usr/local/src    
tar -zxvf zlib-1.2.8.tar.gz   #解压    
cd  zlib-1.2.8 #进入目录    
./configure --prefix=/usr/local/zlib    #配置    
make             #编译    
make install   #安装  

#重新编译php 增加编译参数--with-iconv=/usr/local/iconv 
</pre>
<p>5.4、php5需要的devel 包：</p>
<pre class="brush: bash; title: ; notranslate">
yum install libxml2-devel gd-devel libmcrypt-devel libcurl-devel openssl-devel
</pre>
<p>6、安装php</p>
<pre class="brush: bash; title: ; notranslate">
cd /usr/local/src    
tar -zvxf php-5.3.13.tar.gz    
cd  php-5.3.13    
mkdir -p /usr/local/php5  #建立php安装目录    
#5.3配置    
./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-gd --with-iconv=/usr/local/iconv --with-zlib  --enable-xml --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex  --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --with-jpeg-dir --with-freetype-dir    
#5.5配置    
./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-gd --with-iconv=/usr/local/iconv  --with-zlib --with-zlib-dir=/usr/local/zlib  --enable-xml --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --with-jpeg-dir --with-freetype-dir 

make   #编译    
make install    #安装    

ln -s -f /usr/local/php5/bin/phar.phar /usr/local/php5/bin/phar
</pre>
<pre class="brush: bash; title: ; notranslate">
cp  php.ini-production   /usr/local/php5/etc/php.ini  #复制php配置文件到安装目录 
</pre>
<pre class="brush: bash; title: ; notranslate">
rm -rf /etc/php.ini   #删除系统自带配置文件    
ln -s /usr/local/php5/etc/php.ini  /etc/php.ini    #添加软链接    
cp  /usr/local/php5/etc/php-fpm.conf.default   /usr/local/php5/etc/php-fpm.conf      #拷贝模板文件为php-fpm配置文件    
vim  /usr/local/php5/etc/php-fpm.conf  #编辑    
user = www    #设置php-fpm运行账号为www    
group = www   #设置php-fpm运行组为www    
pid = run/php-fpm.pid    #取消前面的分号    
cp /usr/local/src/php-5.3.13/sapi/fpm/init.d.php-fpm   /etc/rc.d/init.d/php-fpm  #设置 php-fpm开机启动，拷贝php-fpm到启动目录    
chmod +x /etc/rc.d/init.d/php-fpm  #添加执行权限    
chkconfig php-fpm on    #设置开机启动    
</pre>
<pre class="brush: bash; title: ; notranslate">
vim /usr/local/php5/etc/php.ini    #编辑配置文件  
</pre>
<p>找到：&#8221;disable_functions =&#8221;，修改为：</p>
<pre class="brush: bash; title: ; notranslate">
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname     
</pre>
<p>#列出PHP可以禁用的函数，如果某些程序需要用到这个函数，可以删除，取消禁用。</p>
<p>找到：&#8221;;date.timezone =&#8221;，修改为：</p>
<pre class="brush: bash; title: ; notranslate">
date.timezone = PRC   #设置时区   
</pre>
<p>找到：&#8221;expose_php = On&#8221;，修改为：</p>
<pre class="brush: bash; title: ; notranslate">
修改为：expose_php = OFF  #禁止显示php版本的信息    
</pre>
<p>注意：在编译PHP的过程中可能会报UNDEFINED REFERENCE TO `LIBICONV_OPEN 无法编译PHP LIBICONV错误</p>
<p>php5.4 php-fpm重启方法</p>
<p>master进程可以理解以下信号<br />
INT, TERM 立刻终止<br />
QUIT 平滑终止<br />
USR1 重新打开日志文件<br />
USR2 平滑重载所有worker进程并重新载入配置和二进制模块</p>
<p>示例：<br />
php-fpm 启动：</p>
<pre class="brush: bash; title: ; notranslate">
/usr/local/php5/sbin/php-fpm
</pre>
<p>php-fpm 关闭：</p>
<pre class="brush: bash; title: ; notranslate">
kill -INT `cat /usr/local/php5/var/run/php-fpm.pid`
</pre>
<p>php-fpm 重启：</p>
<pre class="brush: bash; title: ; notranslate">
kill -USR2 `cat /usr/local/php5/var/run/php-fpm.pid`
</pre>
<p>查看php-fpm进程数：</p>
<pre class="brush: bash; title: ; notranslate">
ps aux | grep -c php-fpm
</pre>
<p>6.1、phpredis扩展安装</p>
<pre class="brush: bash; title: ; notranslate">
cd /usr/local/src/phpredis-master
/usr/local/php5/bin/phpize
./configure --with-php-config=/usr/local/php5/bin/php-config
make
make install
</pre>
<p>增加phpredis扩展</p>
<pre class="brush: bash; title: ; notranslate">
vim /usr/local/php5/etc/php.ini
</pre>
<p>在后面增加</p>
<pre class="brush: bash; title: ; notranslate">
extension=redis.so
</pre>
<p>测试</p>
<pre class="brush: php; title: ; notranslate">
$objRedis = new Redis();
$objRedis&gt;connect('127.0.0.1',6379)
$objRedis-&gt;set('test','ok');
echo $objRedis-&gt;get('test');
</pre>
<p>7、配置nginx支持php</p>
<pre class="brush: bash; title: ; notranslate">
vim /usr/local/nginx/conf/nginx.conf      #编辑配置文件     
</pre>
<pre class="brush: bash; title: ; notranslate">
user   www  www;          #首行user去掉注释,修改Nginx运行组为www www；必须与/usr/local/php5/etc/php-fpm.conf中的user,group配置相同，否则php运行出错    
index  index.php  index.html index.htm;    #添加index.php    
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000    
#    
location ~ \.php$ {    
   root           html;#此处和server下面root保持一致,默认为html    
   fastcgi_pass   127.0.0.1:9000;    
   fastcgi_index  index.php;    
   fastcgi_param  SCRIPT_FILENAME   /usr/local/nginx/html/$fastcgi_script_name;    
   include        fastcgi_params;          
</pre>
<p>注意：取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为/data/webroot/(此为网站根目录绝对路径)$fastcgi_script_name</p>
<pre class="brush: bash; title: ; notranslate">
/etc/init.d/nginx restart  #重启nginx  
</pre>
<p><a href="http://www.mudbest.com/vmware%e4%b8%8bcentos%e7%9a%84lnmp%e7%bc%96%e8%be%91%e5%ae%89%e8%a3%85/cmake-2-8-8-tar/" rel="attachment wp-att-2053">cmake-2.8.8.tar</a></p>
<p><a href="http://www.mudbest.com/vmware%e4%b8%8bcentos%e7%9a%84lnmp%e7%bc%96%e8%be%91%e5%ae%89%e8%a3%85/nginx-1-4-7-tar/" rel="attachment wp-att-2054">nginx-1.4.7.tar</a><br />
<a href="http://www.mudbest.com/vmware%e4%b8%8bcentos%e7%9a%84lnmp%e7%bc%96%e8%be%91%e5%ae%89%e8%a3%85/phpredis-master/" rel="attachment wp-att-2077">phpredis-master扩展</a></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>由于binlog未及时清理导致硬盘爆满无法启动mysql</title>
		<link>https://www.mudbest.com/%e7%94%b1%e4%ba%8ebinlog%e6%9c%aa%e5%8f%8a%e6%97%b6%e6%b8%85%e7%90%86%e5%af%bc%e8%87%b4%e7%a1%ac%e7%9b%98%e7%88%86%e6%bb%a1%e6%97%a0%e6%b3%95%e5%90%af%e5%8a%a8mysql/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Sat, 08 Feb 2014 05:04:38 +0000</pubDate>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[binlog]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=2017</guid>

					<description><![CDATA[这几天由于太忙了，7天没有观察服务器了，今天用手机打开网站发现报mysql.sock丢失的错误，刚开始以为是m ... <a title="由于binlog未及时清理导致硬盘爆满无法启动mysql" class="read-more" href="https://www.mudbest.com/%e7%94%b1%e4%ba%8ebinlog%e6%9c%aa%e5%8f%8a%e6%97%b6%e6%b8%85%e7%90%86%e5%af%bc%e8%87%b4%e7%a1%ac%e7%9b%98%e7%88%86%e6%bb%a1%e6%97%a0%e6%b3%95%e5%90%af%e5%8a%a8mysql/" aria-label="阅读 由于binlog未及时清理导致硬盘爆满无法启动mysql">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>这几天由于太忙了，7天没有观察服务器了，今天用手机打开网站发现报mysql.sock丢失的错误，刚开始以为是my.ini修改的原因，用df一看，/dev/xvda1的占用已经100%了，这样的情况还能启动mysql就真的逆天了。这时候想到了用sql删除binlog，但是根本进不去控制台，连安全模式都启动不了，以下是具体操作。<span id="more-2017"></span></p>
<p>分配了20G的硬盘，活生生的被binlog吃完了。</p>
<pre class="brush: sql; title: ; notranslate">
tail -f /opt/server/mysql/var/slyar.err
</pre>
<p>看了一下Mysql的错误日志，意外状况出现……</p>
<pre class="brush: sql; title: ; notranslate">
Disk is full writing './mysql-bin.~rec~' (Errcode: 28). Waiting for someone to free space... (Expect up to 60 secs delay for server to continue after freeing disk space)
</pre>
<p>神奇的情况，居然是因为磁盘满了无法写入日志导致Mysql起不来。</p>
<pre class="brush: sql; title: ; notranslate">
root@hkshadow# df 
Filesystem 1K-blocks Used Available Use% Mounted on 
/dev/xvda1 20641404 20641404 0 100% / 
</pre>
<p>基本上就是被这些玩意占满了，查了一下Mysql手册发现这些东西也没啥用，直接删除(此操作最好在Mysql服务停止时进行，因为当前日志使用的话无法删除，而且有可能导致一些其它问题，不过用mysql>reset master命令可以直接清空日志)。</p>
<pre class="brush: sql; title: ; notranslate">/opt/server/mysql/var/
root@hkshadow# cd 
root@hkshadow# rm -f mysql-bin.*
</pre>
<p>如果一定要使用binlog又担心时间久了没及时清理的话，可以在my.ini里设置binlog过期时间。</p>
<pre class="brush: sql; title: ; notranslate">
expire_logs_days = 7
</pre>
<p>如果不需要binlog的话可以关闭掉，避免未来得及监控导致的系列问题。</p>
<pre class="brush: sql; title: ; notranslate">
vim /etc/my.cnf
</pre>
<p>注释掉下面2行</p>
<pre class="brush: sql; title: ; notranslate">
log-bin=mysql-bin
binlog_format=mixed
</pre>
<p>然后重启即可</p>
<pre class="brush: sql; title: ; notranslate">
/etc/init.d/mysqld restart
</pre>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Mysql数据库访问权限控制</title>
		<link>https://www.mudbest.com/mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90%e6%8e%a7%e5%88%b6/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Mon, 11 Mar 2013 11:34:12 +0000</pubDate>
				<category><![CDATA[Mysql]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1800</guid>

					<description><![CDATA[mysql默认的root是不允许远程访问的，只能localhost本地之间的数据访问，如果要想使外部的任何一个 ... <a title="Mysql数据库访问权限控制" class="read-more" href="https://www.mudbest.com/mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e8%ae%bf%e9%97%ae%e6%9d%83%e9%99%90%e6%8e%a7%e5%88%b6/" aria-label="阅读 Mysql数据库访问权限控制">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>mysql默认的root是不允许远程访问的，只能localhost本地之间的数据访问，如果要想使外部的任何一个ip访问数据权限，可执行以下语句：</p>
<p>也可针对指定ip开放访问权限。</p>
<pre class="brush: sql; title: ; notranslate">
CREATE USER 'root'@'%' IDENTIFIED BY PASSWORD '*7B2F14D9BB629E334CD49A1028BD85750F7D3530';
GRANT ALL PRIVILEGES ON * . * TO 'root'@'%' IDENTIFIED BY PASSWORD '*7B2F14D9BB629E334CD49A1028BD85750F7D3530' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;
</pre>
<p>附：</p>
<p>MySQL的Grant命令(MySQL 5.0 及以上版本)。</p>
<p>MySQL 赋予用户权限命令的简单格式可概括为：<span id="more-1800"></span></p>
<pre class="brush: sql; title: ; notranslate">
grant 权限 on 数据库对象 to 用户
</pre>
<p><strong>一、grant 普通数据用户，查询、插入、更新、删除 数据库中所有表数据的权利。</strong></p>
<pre class="brush: sql; title: ; notranslate">
grant select on testdb.* to common_user@'%'
grant insert on testdb.* to common_user@'%'
grant update on testdb.* to common_user@'%'
grant delete on testdb.* to common_user@'%'
</pre>
<p>或者，用一条 MySQL 命令来替代：</p>
<pre class="brush: sql; title: ; notranslate">
grant select, insert, update, delete on testdb.* to common_user@'%'
</pre>
<p><strong>二、grant 数据库开发人员，创建表、索引、视图、存储过程、函数。。。等权限。</strong><br />
grant 创建、修改、删除 MySQL 数据表结构权限。</p>
<pre class="brush: sql; title: ; notranslate">
grant create on testdb.* to developer@'192.168.0.%';
grant alter  on testdb.* to developer@'192.168.0.%';
grant drop   on testdb.* to developer@'192.168.0.%';
</pre>
<p>grant 操作 MySQL 外键权限。</p>
<pre class="brush: sql; title: ; notranslate">
grant references on testdb.* to developer@'192.168.0.%';
</pre>
<p>grant 操作 MySQL 临时表权限。</p>
<pre class="brush: sql; title: ; notranslate">
grant create temporary tables on testdb.* to developer@'192.168.0.%';
</pre>
<p>grant 操作 MySQL 索引权限。</p>
<pre class="brush: sql; title: ; notranslate">
grant index on testdb.* to developer@'192.168.0.%';
</pre>
<p>grant 操作 MySQL 视图、查看视图源代码 权限。</p>
<pre class="brush: sql; title: ; notranslate">
grant create view on testdb.* to developer@'192.168.0.%';
grant show   view on testdb.* to developer@'192.168.0.%';
</pre>
<p>grant 操作 MySQL 存储过程、函数 权限。</p>
<pre class="brush: sql; title: ; notranslate">
grant create routine on testdb.* to developer@'192.168.0.%'; -- now, can show procedure status
grant alter  routine on testdb.* to developer@'192.168.0.%'; -- now, you can drop a procedure
grant execute on testdb.* to developer@'192.168.0.%';
</pre>
<p><strong>三、grant 普通 DBA 管理某个 MySQL 数据库的权限。</strong><br />
其中，关键字 “privileges” 可以省略。</p>
<pre class="brush: sql; title: ; notranslate">
grant all privileges on testdb to dba@'localhost'
</pre>
<p><strong>四、grant 高级 DBA 管理 MySQL 中所有数据库的权限。</strong></p>
<pre class="brush: sql; title: ; notranslate">
grant all on *.* to dba@'localhost'
</pre>
<p><strong></p>
<p>五、MySQL grant 权限，分别可以作用在多个层次上。</strong><br />
1. grant 作用在整个 MySQL 服务器上：</p>
<pre class="brush: sql; title: ; notranslate">
grant select on *.* to dba@localhost; -- dba 可以查询 MySQL 中所有数据库中的表。
grant all on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有数据库
</pre>
<p>2. grant 作用在单个数据库上：</p>
<pre class="brush: sql; title: ; notranslate">
grant select on testdb.* to dba@localhost; -- dba 可以查询 testdb 中的表。
</pre>
<p>3. grant 作用在单个数据表上：</p>
<pre class="brush: sql; title: ; notranslate">
grant select, insert, update, delete on testdb.orders to dba@localhost;
</pre>
<p>这里在给一个用户授权多张表时，可以多次执行以上语句。例如：</p>
<pre class="brush: sql; title: ; notranslate">
grant select(user_id,username) on smp.users to mo_user@'%' identified by '123345';
grant select on smp.mo_sms to mo_user@'%' identified by '123345';
</pre>
<p>4. grant 作用在表中的列上：</p>
<pre class="brush: sql; title: ; notranslate">
grant select(id, se, rank) on testdb.apache_log to dba@localhost;
</pre>
<p>5. grant 作用在存储过程、函数上：</p>
<pre class="brush: sql; title: ; notranslate">
grant execute on procedure testdb.pr_add to 'dba'@'localhost'
grant execute on function testdb.fn_add to 'dba'@'localhost'
</pre>
<p><strong>六、查看 MySQL 用户权限</strong><br />
查看当前用户（自己）权限：</p>
<pre class="brush: sql; title: ; notranslate">
show grants;
</pre>
<p>查看其他 MySQL 用户权限：</p>
<pre class="brush: sql; title: ; notranslate">
show grants for dba@localhost;
</pre>
<p>七、撤销已经赋予给 MySQL 用户权限的权限。<br />
revoke 跟 grant 的语法差不多，只需要把关键字 “to” 换成 “from” 即可：</p>
<pre class="brush: sql; title: ; notranslate">
grant  all on *.* to   dba@localhost;
revoke all on *.* from dba@localhost;
</pre>
<p>八、MySQL grant、revoke 用户权限注意事项<br />
1. grant, revoke 用户权限后，该用户只有重新连接 MySQL 数据库，权限才能生效。<br />
2. 如果想让授权的用户，也可以将这些权限 grant 给其他用户，需要选项 “grant option“</p>
<pre class="brush: sql; title: ; notranslate">
grant select on testdb.* to dba@localhost with grant option;
</pre>
<p>这个特性一般用不到。实际中，数据库权限最好由 DBA 来统一管理。</p>
<p>遇到 SELECT command denied to user &#8216;用户名&#8217;@&#8217;主机名&#8217; for table &#8216;表名&#8217; 这种错误，解决方法是需要把吧后面的表名授权，即是要你授权核心数据库也要。</p>
<p>我遇到的是SELECT command denied to user &#8216;my&#8217;@&#8217;%&#8217; for table &#8216;proc&#8217;，是调用存储过程的时候出现，原以为只要把指定的数据库授权就行了，什么存储过程、函数等都不用再管了，谁知道也要把数据库mysql的proc表授权。</p>
<p>mysql授权表共有5个表：user、db、host、tables_priv和columns_priv。</p>
<p>授权表的内容有如下用途：<br />
user Table：<br />
user表列出可以连接服务器的用户及其口令，并且它指定他们有哪种全局（超级用户）权限。在user表启用的任何权限均是全局权限，并适用于所有数据库。例如，如果你启用了DELETE权限，在这里列出的用户可以从任何表中删除记录，所以在你这样做之前要认真考虑。</p>
<p>db Table：<br />
db表列出数据库，而用户有权限访问它们。在这里指定的权限适用于一个数据库中的所有表。</p>
<p>host Table：<br />
host表与db表结合使用在一个较好层次上控制特定主机对数据库的访问权限，这可能比单独使用db好些。这个表不受GRANT和REVOKE语句的影响，所以，你可能发觉你根本不是用它。</p>
<p>tables_priv Table：<br />
tables_priv表指定表级权限，在这里指定的一个权限适用于一个表的所有列。</p>
<p>columns_priv Table：<br />
columns_priv表指定列级权限。这里指定的权限适用于一个表的特定列。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Mysql字段的增加和删除</title>
		<link>https://www.mudbest.com/mysql%e5%ad%97%e6%ae%b5%e7%9a%84%e5%a2%9e%e5%8a%a0%e5%92%8c%e5%88%a0%e9%99%a4/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Thu, 20 Dec 2012 11:03:28 +0000</pubDate>
				<category><![CDATA[Mysql]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[字段修改]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1738</guid>

					<description><![CDATA[本来就没有记单词的习惯，平时触及Mysql操作不是很多，很多语句也就不怎么记得了，偶尔的处理一下这样的需求，那 ... <a title="Mysql字段的增加和删除" class="read-more" href="https://www.mudbest.com/mysql%e5%ad%97%e6%ae%b5%e7%9a%84%e5%a2%9e%e5%8a%a0%e5%92%8c%e5%88%a0%e9%99%a4/" aria-label="阅读 Mysql字段的增加和删除">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>本来就没有记单词的习惯，平时触及Mysql操作不是很多，很多语句也就不怎么记得了，偶尔的处理一下这样的需求，那就要语句嘛，好吧，下面贴起来。<span id="more-1738"></span></p>
<p>增加字段的语句：</p>
<pre class="brush: sql; title: ; notranslate">
alter table `user_movement_log`   
Add column GatewayId int not null default 0 AFTER `Regionid` (在哪个字段后面添加)
</pre>
<p>删除字段的语句：</p>
<pre class="brush: sql; title: ; notranslate">
alter table `user_movement_log` drop column Gatewayid  
</pre>
<p>调整字段顺序的语句：</p>
<pre class="brush: sql; title: ; notranslate">
ALTER TABLE `user_movement_log` CHANGE `GatewayId` `GatewayId` int not null default 0 AFTER RegionID  

//主键  

alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);//增加一个新列  

alter table t2 add d timestamp;  
alter table infos add ex tinyint not null default '0';//删除列  

alter table t2 drop column c;//重命名列  

alter table t1 change a b integer;  
//改变列的类型  

alter table t1 change b b bigint not null;  
alter table infos change list list tinyint not null default '0';  
//重命名表  

alter table t1 rename t2;加索引  

mysql&gt; alter table tablename change depno depno int(5) not null;  
mysql&gt; alter table tablename add index 索引名 (字段名1&#x5B;，字段名2 …]);  
mysql&gt; alter table tablename add index emp_name (name);加主关键字的索引  

mysql&gt; alter table tablename add primary key(id);加唯一限制条件的索引  

mysql&gt; alter table tablename add unique emp_name2(cardnumber);删除某个索引  

mysql&gt;alter table tablename drop index emp_name;修改表：  
</pre>
<p>控制台增加字段：</p>
<pre class="brush: sql; title: ; notranslate">
mysql&gt; ALTER TABLE table_name ADD field_name field_type;修改原字段名称及类型：  
mysql&gt; ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;删除字段：   
mysql&gt; ALTER TABLE table_name DROP field_name; 
</pre>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>MySQL Workbench的使用</title>
		<link>https://www.mudbest.com/mysql-workbench%e7%9a%84%e4%bd%bf%e7%94%a8/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Wed, 21 Nov 2012 03:18:35 +0000</pubDate>
				<category><![CDATA[Mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[MySQL Workbench]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[设计]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1672</guid>

					<description><![CDATA[MySQL Workbench 是 MySQL AB 最近释放的可视数据库设计工具。这个工具是设计 MySQL ... <a title="MySQL Workbench的使用" class="read-more" href="https://www.mudbest.com/mysql-workbench%e7%9a%84%e4%bd%bf%e7%94%a8/" aria-label="阅读 MySQL Workbench的使用">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>MySQL Workbench 是 MySQL AB 最近释放的可视数据库设计工具。这个工具是设计 MySQL 数据库的专用工具。<br />
在 MySQL Workbench 中建立的被称之为物理数据模型。一个物理数据模型是针对一个特定 RDBMS 产品的数据模型；本文中的模型将具有一些 MySQL 的独特规范。我们可以使用它生成（forward-engineer）数据库对象，除了包含表和列（字段）外，也可以包含视图。<br />
MySQL Workbench 拥有很多的功能和特性；这篇由 Djoni Darmawikarta写的<span id="more-1672"></span>文章通过一个示例展现了其中的一些。我们将针对一个订单系统建立一个物理数据模型，这里的订单系统可以是销售单也可以是订单，并且使用  forward-engineer（正向引擎） 将我们的模型生成为一个 MySQL 数据库。<br />
我们的示例中使用 MySQL Workbench 创建的物理模型看上去就像下图这样：</p>
<p>小知识:<br />
mysql的pk nn uq bin un zf ai各代表的意思<br />
[intrinsic column flags] (基本字段类型标识)</p>
<p>&#8211; PK: primary key (column is part of a pk) 主键<br />
&#8211; NN: not null (column is nullable) 非空<br />
&#8211; UQ: unique (column is part of a unique key) 唯一<br />
&#8211; AI: auto increment (the column is auto incremented when rows are inserted) 自增</p>
<p>[additional data type flags, depend on used data type] 扩展数据类型标记</p>
<p>&#8211; BIN: binary (if dt is a blob or similar, this indicates that is binary data, rather than text) 二进制(比text更大的二进制数据)<br />
&#8211; UN: unsigned (for integer types, see docs: “10.2. Numeric Types”) 整数<br />
&#8211; ZF: zero fill (rather a display related flag, see docs: “10.2. Numeric Types”)值中最有意义的字节总为0，并且不保存。 </p>
<p><img decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image1.png" alt="" title="workbench-article-image1" width="500" height="343" class="alignnone size-full wp-image-1673" /></p>
<p>索引简单介绍：<br />
PRIMARY, INDEX, UNIQUE 这3种是一类<br />
PRIMARY 主键。 就是 唯一 且 不能为空。<br />
INDEX 索引，普通的<br />
UNIQUE 唯一索引。 不允许有重复。<br />
FULLTEXT 是全文索引，用于在一篇文章中，检索文本信息的。</p>
<p>举个例子来说，比如你在为某商场做一个会员卡的系统。<br />
这个系统有一个会员表<br />
有下列字段：<br />
会员编号   INT<br />
会员姓名   VARCHAR(10)<br />
会员身份证号码   VARCHAR(18)<br />
会员电话   VARCHAR(10)<br />
会员住址   VARCHAR(50)<br />
会员备注信息  TEXT</p>
<p>那么这个 会员编号，作为主键，使用 PRIMARY<br />
会员姓名 如果要建索引的话，那么就是普通的 INDEX<br />
会员身份证号码   如果要建索引的话，那么可以选择 UNIQUE （唯一的，不允许重复）<br />
会员备注信息 ， 如果需要建索引的话，可以选择 FULLTEXT，全文搜索。</p>
<p>不过 FULLTEXT 用于搜索很长一篇文章的时候，效果最好。<br />
用在比较短的文本，如果就一两行字的，普通的 INDEX 也可以。</p>
<p>创建订单方案(ORDER Schema)<br />
首先让我们来创建一个保存订单物理模型的方案。点击 + 按钮（红色标注的地方）</p>
<p><img decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image2.png" alt="" title="workbench-article-image2" width="500" height="343" class="alignnone size-full wp-image-1674" /></p>
<p>更改新的方案默认名称为订单。注意，当你键入方案名时，在 Physical Schemata 上的标签名也会随之改变——这是一个很好的特性。</p>
<p>订单方案被增加到目录（Catalog）中 (图中红色圈住的部分)。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image3.png" alt="" title="workbench-article-image3" width="500" height="343" class="alignnone size-full wp-image-1675" /></p>
<p>重命名方案后关闭 schema 窗口。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image4.png" alt="" title="workbench-article-image4" width="500" height="145" class="alignnone size-full wp-image-1676" /></p>
<p>创建订单表<br />
我们现在创建订单模型中的三个表：ORDER 表以及它的两个子表 SALES_ORDER 和PURCHASE_ORDER。首先，确信你已经选择了 ORDER 方案的标签，这样，我们创建的表才会包含于这个方案。</p>
<p>我们将要创建的表是作为 EER 图表展示的(EER = Enhanced Entity Relationship)。所以，双击 Add Diagram 按钮。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image5.png" alt="" title="workbench-article-image5" width="223" height="265" class="alignnone size-full wp-image-1677" /></p>
<p>单击 Table 图标，然后移动鼠标到 EER Diagram 区域，在你想放置第一个表的位置上单击鼠标。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image6.png" alt="" title="workbench-article-image6" width="194" height="188" class="alignnone size-full wp-image-1678" /></p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image7.png" alt="" title="workbench-article-image7" width="432" height="298" class="alignnone size-full wp-image-1679" /></p>
<p>对于其他两个表，重复上面的操作。你可以通过拖拽来移动表的位置。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image8.png" alt="" title="workbench-article-image8" width="500" height="434" class="alignnone size-full wp-image-1680" srcset="https://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image8.png 500w, https://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image8-495x430.png 495w" sizes="auto, (max-width: 500px) 100vw, 500px" /></p>
<p>下一步，我们要对 table1 做一些操作，这些操作是通过 Workbench 的表编辑器完成的。要打开表编辑器，只需右键选择 table1 并选择 Edit Table 菜单。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image9.png" alt="" title="workbench-article-image9" width="500" height="249" class="alignnone size-full wp-image-1681" /></p>
<p>键入 table1 的表名 ORDER 。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image10.png" alt="" title="workbench-article-image10" width="433" height="335" class="alignnone size-full wp-image-1682" /></p>
<p>接下来，增加列（字段）。选择 Columns 标签。将列名(字段名) idORDER 更改为  ORDER_NO.</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image11.png" alt="" title="workbench-article-image11" width="500" height="154" class="alignnone size-full wp-image-1683" /></p>
<p>在下拉列表框中选择数据类型 INT 。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image12.png" alt="" title="workbench-article-image12" width="500" height="497" class="alignnone size-full wp-image-1684" srcset="https://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image12.png 500w, https://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image12-432x430.png 432w" sizes="auto, (max-width: 500px) 100vw, 500px" /></p>
<p>我们希望 ORDER_NO 列的值可以被 MySQL 数据库自动控制，所以，我们选定 AI 列 (Auto Increment——自增量).</p>
<p>AI 是 MySQL 数据库的一个特性。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image13.png" alt="" title="workbench-article-image13" width="457" height="125" class="alignnone size-full wp-image-1685" /></p>
<p>你也可以指定表的其他物理属性，例如它的 Collation 属性；当然可以指定表的其他高级选项，例如 trigger 和 portioning （分别对应 Trigger 和 Partioning 标签）。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image14.png" alt="" title="workbench-article-image14" width="277" height="156" class="alignnone size-full wp-image-1686" /></p>
<p>注意，这时，在 diagram 中我们表 table1 已经改变为 ORDER，并且，它有一个列（字段）ORDER_NO。在目录中，你也可以看到有三个表。</p>
<p>在表右侧的黑点，表示它们包含在一个图表中。</p>
<p>如果你展开 ORDER ，你可以看到 ORDER_NO 列。因为我们定义它为主键，所以在它左侧有一个 key 图标</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image16.png" alt="" title="workbench-article-image16" width="181" height="163" class="alignnone size-full wp-image-1687" /></p>
<p>回到表设计器，增加其他两列（字段）：ORDER_DATE 和 ORDER_TYPE。ORDER_TYPE 可以有两个值：S 表示销售订单，P 表示采购订单。由于销售订单是更常用的，所以我们指定列（字段）的默认值为 S 。</p>
<p>你可以在最后一列的下面白色区域双击鼠标来增加下一个字段</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image17.png" alt="" title="workbench-article-image17" width="457" height="127" class="alignnone size-full wp-image-1688" /></p>
<p>使用同样的方式来创建 SALES_ORDER 表及其中的列（字段）。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image18.png" alt="" title="workbench-article-image18" width="500" height="337" class="alignnone size-full wp-image-1689" /></p>
<p>最后，创建 PURCHASE_ORDER 表及其中的列（字段）。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image19.png" alt="" title="workbench-article-image19" width="500" height="345" class="alignnone size-full wp-image-1690" /></p>
<p>创建关系<br />
我们已经创建了三个表。到这里并没有结束；我们仍旧需要创建它们的 关系。</p>
<p>SALES_ORDER 是 ORDER 的子表，意味着它们是 1:1 ，SALES_ORDER 为子表， ORDER 为父表，并将 ORDER 的键移动到 SALES_ORDER 。所以，选择（单击）1:1 identifying relationship 图标，然后在 SALES_ORDER 表上单击，再在 ORDER 表上单击。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image20.png" alt="" title="workbench-article-image20" width="278" height="231" class="alignnone size-full wp-image-1691" /></p>
<p>注意，当你在单击表时，图标将变为带有 1:1 关系的手型。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image21.png" alt="" title="workbench-article-image21" width="229" height="219" class="alignnone size-full wp-image-1692" /></p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image22.png" alt="" title="workbench-article-image22" width="217" height="205" class="alignnone size-full wp-image-1693" /></p>
<p>1:1 关系就是这样设置的；ORDER_NO 主键被移动到 SALES_ORDER 表并作为它的主键。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image23.png" alt="" title="workbench-article-image23" width="225" height="278" class="alignnone size-full wp-image-1694" /></p>
<p>下一步，创建 PURCHASE_ORDER 到 ORDER 的关系，它仍旧是 1:1 关系。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image24.png" alt="" title="workbench-article-image24" width="500" height="343" class="alignnone size-full wp-image-1695" /></p>
<p>我们现在已经完成了表及表之间关系的设计；将我们的模型保存为 ORDER.mwb.</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image25.png" alt="" title="workbench-article-image25" width="276" height="408" class="alignnone size-full wp-image-1696" /></p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image26.png" alt="" title="workbench-article-image26" width="500" height="342" class="alignnone size-full wp-image-1697" /></p>
<p>生成 DDL 和数据库<br />
最终在本文设计数据模型的目的是为了建立 MySQL 数据库。我们将首先生成 DDL(SQL CREATE script)，然后执行这个脚本。</p>
<p>从 File | Export 菜单中，选择 Forward Engineer SQL CREATE Script.</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image27.png" alt="" title="workbench-article-image27" width="500" height="356" class="alignnone size-full wp-image-1698" /></p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image28.png" alt="" title="workbench-article-image28" width="500" height="378" class="alignnone size-full wp-image-1699" /></p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image29.png" alt="" title="workbench-article-image29" width="500" height="383" class="alignnone size-full wp-image-1700" /></p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image30.png" alt="" title="workbench-article-image30" width="500" height="283" class="alignnone size-full wp-image-1701" /></p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image31.png" alt="" title="workbench-article-image31" width="500" height="408" class="alignnone size-full wp-image-1702" /></p>
<p>你也可以在其中来查看表是否已经被创建。</p>
<p><img loading="lazy" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/11/workbench-article-image32.png" alt="" title="workbench-article-image32" width="333" height="325" class="alignnone size-full wp-image-1703" /></p>
<p>全图文演示了在 MySQL Workbench 中如何可视的建立 MySQL 物理数据模型，并使用它来创建一个 MySQL 数据库。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>windows下手动搭建Apache+PHP+MySQL</title>
		<link>https://www.mudbest.com/windows%e4%b8%8b%e6%89%8b%e5%8a%a8%e6%90%ad%e5%bb%baapachephpmysql/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Tue, 30 Oct 2012 06:09:37 +0000</pubDate>
				<category><![CDATA[分享]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[下载]]></category>
		<category><![CDATA[安装]]></category>
		<category><![CDATA[配置]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1642</guid>

					<description><![CDATA[一、下载 http://pan.baidu.com/share/link?shareid=95859&#038 ... <a title="windows下手动搭建Apache+PHP+MySQL" class="read-more" href="https://www.mudbest.com/windows%e4%b8%8b%e6%89%8b%e5%8a%a8%e6%90%ad%e5%bb%baapachephpmysql/" aria-label="阅读 windows下手动搭建Apache+PHP+MySQL">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p><strong>一、下载</strong><br />
http://pan.baidu.com/share/link?shareid=95859&#038;uk=2349308099</p>
<p><strong>二、安装、解压</strong><br />
 1. Apache</p>
<p>　　主要配置Network Domain、Server Name、Email地址（我依次填的是localhost、localhost、邮箱随意）以及Apache服务的占用端口，默认为80端口，你可以根据需要配置在其他端口，Apache的安装目录你可以使用默认目录或根据需要自行选择安装目录（我安装到 D:\Apache）。<span id="more-1642"></span></p>
<p>　　在完成apache服务的安装后，在游览器中输入http://localhost/，出现It’s work!字样，说明Apache服务安装成功了。</p>
<p>2. PHP</p>
<p>　　只要解压php-5.3.4-Win32-VC6-x86.zip并重名为文件夹为php到某个盘即可完成PHP安装（我是将其解压复制到D盘目录下 D:\web_server\php5.3）</p>
<p> 3. MySQL</p>
<p>　　点击Mysql安装程序自动安装，在此期间你可以根据需要选择Mysql数据库的安装目录（我安装到 D:\web_server\MySQL），MySQL 安装完后会启动向导，你可以先取消，下一部的配置里有说。 </p>
<p><strong>三、配置 </strong><br />
<em><br />
 1. Apache（Apache配置文件httpd.conf在Apache安装目录下的conf文件夹里面）</em></p>
<p>(1)默认Apache服务器执行WEB主程序的目录为Apache2.2/htdocs，所以当你的WEB主程序目录变更时，我们需要修改相应的Apache配置（我改到D:\web_server\apache2.2\htdocs），即用文本编辑软件打开 httpd.conf 将 </p>
<p>找到</p>
<pre class="brush: plain; title: ; notranslate">
&lt;Directory /&gt;
	Options FollowSymLinks
	AllowOverride None
	Order deny,allow
	Deny from all
&lt;/Directory&gt;
</pre>
<p>将其注释掉</p>
<pre class="brush: plain; title: ; notranslate">
#&lt;Directory /&gt;
#	Options FollowSymLinks
#	AllowOverride None
#	Order deny,allow
#	Deny from all
#&lt;/Directory&gt;
</pre>
<p>找到</p>
<pre class="brush: plain; title: ; notranslate">
&lt;Directory &quot;D:/web_server/apache2.2/htdocs&quot;&gt;
	#
	# Possible values for the Options directive are &quot;None&quot;, &quot;All&quot;,
	# or any combination of:
	#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
	#
	# Note that &quot;MultiViews&quot; must be named *explicitly* --- &quot;Options All&quot;
	# doesn't give it to you.
	#
	# The Options directive is both complicated and important.  Please see
	# http://httpd.apache.org/docs/2.2/mod/core.html#options
	# for more information.
	#
	Options Indexes FollowSymLinks

	#
	# AllowOverride controls what directives may be placed in .htaccess files.
	# It can be &quot;All&quot;, &quot;None&quot;, or any combination of the keywords:
	#   Options FileInfo AuthConfig Limit
	#
	AllowOverride None

	#
	# Controls who can get stuff from this server.
	#
	Order allow,deny
	Allow from all

&lt;/Directory&gt;
</pre>
<p>将其注释</p>
<pre class="brush: plain; title: ; notranslate">
#&lt;Directory &quot;D:/web_server/apache2.2/htdocs&quot;&gt;
	#
	# Possible values for the Options directive are &quot;None&quot;, &quot;All&quot;,
	# or any combination of:
	#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
	#
	# Note that &quot;MultiViews&quot; must be named *explicitly* --- &quot;Options All&quot;
	# doesn't give it to you.
	#
	# The Options directive is both complicated and important.  Please see
	# http://httpd.apache.org/docs/2.2/mod/core.html#options
	# for more information.
	#
#	Options Indexes FollowSymLinks

	#
	# AllowOverride controls what directives may be placed in .htaccess files.
	# It can be &quot;All&quot;, &quot;None&quot;, or any combination of the keywords:
	#   Options FileInfo AuthConfig Limit
	#
#	AllowOverride None

	#
	# Controls who can get stuff from this server.
	#
#	Order allow,deny
#	Allow from all

#&lt;/Directory&gt;
</pre>
<p>找到</p>
<pre class="brush: plain; title: ; notranslate">
#Include conf/extra/httpd-vhosts.conf
</pre>
<p>将注释去掉</p>
<pre class="brush: plain; title: ; notranslate">
Include conf/extra/httpd-vhosts.conf
</pre>
<p>找到</p>
<pre class="brush: plain; title: ; notranslate">
#LoadModule rewrite_module modules/mod_rewrite.so
</pre>
<p>将注释去掉</p>
<pre class="brush: plain; title: ; notranslate">
#LoadModule rewrite_module modules/mod_rewrite.so
</pre>
<p>找到</p>
<pre class="brush: plain; title: ; notranslate">
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
</pre>
<p>回车下一行增加以下内容</p>
<pre class="brush: plain; title: ; notranslate">
LoadModule php5_module &quot;D:/web_server/php5.3/php5apache2_2.dll&quot;
PHPIniDir &quot;D:/web_server/php5.3/&quot;
</pre>
<p>说明：在PHP目录下可以看到多个php5apache的DLL文件，由于我使用的是Apache2.2.17，所以我们当然需要使用php5apache2_2.dll，接着指定PHP的安装目录以及执行的程序扩展名。 </p>
<p><em> 2. PHP（配置文件php.ini在PHP安装目录下）</em></p>
<p>(1)把php.ini-development改名为php.ini，然后用文本编辑器修改php.ini，接下来的步骤都是在php.ini进行的。</p>
<p>找到</p>
<pre class="brush: plain; title: ; notranslate">
; On windows:

; extension_dir = &quot;ext&quot;
</pre>
<p>修改为</p>
<pre class="brush: plain; title: ; notranslate">
; On windows:

extension_dir = &quot;D:/php/ext&quot;
</pre>
<p>表示指定PHP扩展包的具体目录，以便调用相应的DLL文件，这样就不用讲PHP的一些dll和php.ini复制到windows下了。</p>
<p>由于默认PHP并不支持自动连接Mysql，需开启相应的扩展库功能，比如php_mysql.dll等，即将 </p>
<pre class="brush: plain; title: ; notranslate">
; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.
; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5)
; extension folders as well as the separate PECL DLL download (PHP 5).
; Be sure to appropriately set the extension_dir directive.
;
;extension=php_bz2.dll
extension=php_curl.dll
;extension=php_fileinfo.dll
extension=php_gd2.dll
;extension=php_gettext.dll
;extension=php_gmp.dll
;extension=php_intl.dll
;extension=php_imap.dll
;extension=php_interbase.dll
;extension=php_ldap.dll
extension=php_mbstring.dll
extension=php_exif.dll 
; Must be after mbstring as it depends on it
extension=php_mysql.dll
extension=php_mysqli.dll
;extension=php_oci8.dll      ; Use with Oracle 10gR2 Instant Client
;extension=php_oci8_11g.dll  ; Use with Oracle 11gR2 Instant Client
extension=php_openssl.dll
extension=php_pdo_firebird.dll
extension=php_pdo_mssql.dll
extension=php_pdo_mysql.dll
extension=php_pdo_oci.dll
extension=php_pdo_odbc.dll
extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
extension=php_pgsql.dll
;extension=php_pspell.dll
;extension=php_shmop.dll

; The MIBS data available in the PHP distribution must be installed. 
; See http://www.php.net/manual/en/snmp.installation.php 
;extension=php_snmp.dll

;extension=php_soap.dll
extension=php_sockets.dll
extension=php_sqlite.dll
extension=php_sqlite3.dll
;extension=php_sybase_ct.dll
;extension=php_tidy.dll
;extension=php_xmlrpc.dll
extension=php_xsl.dll
;extension=php_zip.dll

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
</pre>
<p>找到</p>
<pre class="brush: plain; title: ; notranslate">
;session.save_path = &quot;/tmp&quot;
</pre>
<p>修改为</p>
<pre class="brush: plain; title: ; notranslate">
session.save_path = &quot;D:/web_server/php5.3/session&quot;
</pre>
<p>找到</p>
<pre class="brush: plain; title: ; notranslate">
;upload_tmp_dir =
</pre>
<p>修改为</p>
<pre class="brush: plain; title: ; notranslate">
upload_tmp_dir = &quot;D:/web_server/php5.3/temp&quot;
</pre>
<p>找到</p>
<pre class="brush: plain; title: ; notranslate">
;date.timezone =
</pre>
<p>修改为</p>
<pre class="brush: plain; title: ; notranslate">
date.timezone = &quot;Asia/Shanghai&quot;
</pre>
<p><em>3. MySQL</em><br />
点击开始菜单下Mysql Server5.5菜单中的Mysql Server Instance Config Wizard，完成Mysql的配置向导。</p>
<p>点击 Next》 后出现3个选项：选择Mysql服务器的类型</p>
<p>Developer Machine</p>
<p>Server Machine</p>
<p>Dedicated MySQL Machine</p>
<p>我选第2个“Server Machine”</p>
<p>然后又是3个选项：Mysql数据库用途</p>
<p>Multifunctional Database</p>
<p>Transactional Database Only</p>
<p>Non-Transactional Database Only</p>
<p>我选第1个“Multifunctional Database”</p>
<p>选择Mysql数据库的并发连接数</p>
<p>Decision Support （DSS）/OLAP：最大20个连接并发数</p>
<p>Online Transaction Processing（OLTP）：最大500个并发连接数</p>
<p>Manual Setting：自定义</p>
<p>我选自定义，设置为50</p>
<p>选择Mysql服务的端口号，一般默认即可</p>
<p>选择Mysql数据库的字符集（推荐使用UTF8，比较通用，否则容易造成乱码。）</p>
<p>Standard Character Set</p>
<p>Best Support For Multilingualism</p>
<p>Manual Selected Default Character Set / Collation</p>
<p>我选“Manual Selected Default Character Set / Collation”，然后在 Character Set 选择 utf8</p>
<p>设置Mysql为Windows服务</p>
<p>在这里考虑开机速度的问题，我将自动登录Mysql服务取消了，一般你可以选择此选项，如果没有选，你可以使用net start mysql启动Mysql服务（停止就是net stop mysql）。</p>
<p>我的选择是“Install AS Windows Service”和“Include Bin Directory in Windows PATH”勾选，“Launch the MySQL Server automatically”去掉勾选</p>
<p>设置Mysql数据库root用户的密码</p>
<p>执行Mysql服务配置选项</p>
<p>Mysql数据库的配置文件保存在D:\MySQL\my.ini，如果今后有什么变动，你可以修改此文件。 </p>
<p>到此，所有配置完成。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Mysql一些细节性的优化</title>
		<link>https://www.mudbest.com/mysql%e4%b8%80%e4%ba%9b%e7%bb%86%e8%8a%82%e6%80%a7%e7%9a%84%e4%bc%98%e5%8c%96/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Fri, 19 Oct 2012 06:15:48 +0000</pubDate>
				<category><![CDATA[Mysql]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[优化]]></category>
		<category><![CDATA[最优]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1586</guid>

					<description><![CDATA[1、对查询进行优化，应尽量避免全表扫描，首先应考虑在 where 及 order by 涉及的列上建立索引。  ... <a title="Mysql一些细节性的优化" class="read-more" href="https://www.mudbest.com/mysql%e4%b8%80%e4%ba%9b%e7%bb%86%e8%8a%82%e6%80%a7%e7%9a%84%e4%bc%98%e5%8c%96/" aria-label="阅读 Mysql一些细节性的优化">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>1、对查询进行优化，应尽量避免全表扫描，首先应考虑在 where 及 order by 涉及的列上建立索引。</p>
<p>2、应尽量避免在 where 子句中对字段进行 null 值判断，否则将导致引擎放弃使用索引而进行全表扫描，如：</p>
<pre class="brush: sql; title: ; notranslate">
select id from t where num is null
</pre>
<p>可以在num上设置默认值0，确保表中num列没有null值，然后这样查询：</p>
<pre class="brush: sql; title: ; notranslate">
select id from t where num=0
</pre>
<p>3、应尽量避免在 where 子句中使用!=或<>操作符，否则<span id="more-1586"></span>将引擎放弃使用索引而进行全表扫描。</p>
<p>4、应尽量避免在 where 子句中使用 or 来连接条件，否则将导致引擎放弃使用索引而进行全表扫描，如：</p>
<pre class="brush: sql; title: ; notranslate">
select id from t where num=10 or num=20
</pre>
<p>可以这样查询：</p>
<pre class="brush: sql; title: ; notranslate">
select id from t where num=10
union all
select id from t where num=20
</pre>
<p>5、in 和 not in 也要慎用，否则会导致全表扫描，如：</p>
<pre class="brush: sql; title: ; notranslate">
select id from t where num in(1,2,3)
</pre>
<p>对于连续的数值，能用 between 就不要用 in 了：</p>
<pre class="brush: sql; title: ; notranslate">
select id from t where num between 1 and 3
</pre>
<p>6、下面的查询也将导致全表扫描：</p>
<pre class="brush: sql; title: ; notranslate">
select id from t where name like '%abc%'
</pre>
<p>若要提高效率，可以考虑全文检索。</p>
<p>7、如果在 where 子句中使用参数，也会导致全表扫描。因为SQL只有在运行时才会解析局部变量，但优化程序不能将访问计划的选择推迟到运行时；它必须在编译时进行选择。然而，如果在编译时建立访问计划，变量的值还是未知的，因而无法作为索引选择的输入项。如下面语句将进行全表扫描：</p>
<pre class="brush: sql; title: ; notranslate">
select id from t where num=@num 
#&lt;mailto:num=@num&gt;
</pre>
<p>可以改为强制查询使用索引：</p>
<pre class="brush: sql; title: ; notranslate">
select id from t with(index(索引名)) where num=@num 
#&lt;mailto:num=@num&gt;
</pre>
<p>8、应尽量避免在 where 子句中对字段进行表达式操作，这将导致引擎放弃使用索引而进行全表扫描。如：</p>
<pre class="brush: sql; title: ; notranslate">
select id from t where num/2=100
</pre>
<p>应改为:</p>
<pre class="brush: sql; title: ; notranslate">
select id from t where num=100*2
</pre>
<p>9、应尽量避免在where子句中对字段进行函数操作，这将导致引擎放弃使用索引而进行全表扫描。如：</p>
<pre class="brush: sql; title: ; notranslate">
select id from t where substring(name,1,3)='abc'
#-name以abc开头的id
select id from t where datediff(day,createdate,'2005-11-30') = 0 
#-'005-11-30'生成的id
</pre>
<p>应改为:</p>
<pre class="brush: sql; title: ; notranslate">
select id from t where name like 'abc%'
select id from t where createdate &gt;= '2005-11-30' and createdate &lt; '2005-12-1'
</pre>
<p>10、不要在 where 子句中的“=”左边进行函数、算术运算或其他表达式运算，否则系统将可能无法正确使用索引。</p>
<p>11、在使用索引字段作为条件时，如果该索引是复合索引，那么必须使用到该索引中的第一个字段作为条件时才能保证系统使用该索引，否则该索引将不会被使用，并且应尽可能的让字段顺序与索引顺序相一致。</p>
<p>12、不要写一些没有意义的查询，如需要生成一个空表结构：</p>
<pre class="brush: sql; title: ; notranslate">
select col1,col2 into #t from t where 1=0
</pre>
<p>这类代码不会返回任何结果集，但是会消耗系统资源的，应改成这样：</p>
<pre class="brush: sql; title: ; notranslate">
create table #t(…)
</pre>
<p>13、很多时候用 exists 代替 in 是一个好的选择：</p>
<pre class="brush: sql; title: ; notranslate">
select num from a where num in(select num from b)
</pre>
<p>用下面的语句替换：</p>
<pre class="brush: sql; title: ; notranslate">
select num from a where exists(select 1 from b where num=a.num)
</pre>
<p>14、并不是所有索引对查询都有效，SQL是根据表中数据来进行查询优化的，当索引列有大量数据重复时，SQL查询可能不会去利用索引，如一表中有字段sex，male、female几乎各一半，那么即使在sex上建了索引也对查询效率起不了作用。</p>
<p>15、索引并不是越多越好，索引固然可以提高相应的 select 的效率，但同时也降低了 insert 及 update 的效率，因为 insert 或 update 时有可能会重建索引，所以怎样建索引需要慎重考虑，视具体情况而定。一个表的索引数最好不要超过6个，若太多则应考虑一些不常使用到的列上建的索引是否有必要。</p>
<p>16、应尽可能的避免更新 clustered 索引数据列，因为 clustered 索引数据列的顺序就是表记录的物理存储顺序，一旦该列值改变将导致整个表记录的顺序的调整，会耗费相当大的资源。若应用系统需要频繁更新 clustered 索引数据列，那么需要考虑是否应将该索引建为 clustered 索引。</p>
<p>17、尽量使用数字型字段，若只含数值信息的字段尽量不要设计为字符型，这会降低查询和连接的性能，并会增加存储开销。这是因为引擎在处理查询和连接时会逐个比较字符串中每一个字符，而对于数字型而言只需要比较一次就够了。</p>
<p>18、尽可能的使用 varchar/nvarchar 代替 char/nchar ，因为首先变长字段存储空间小，可以节省存储空间，其次对于查询来说，在一个相对较小的字段内搜索效率显然要高些。</p>
<p>19、任何地方都不要使用 select * from t ，用具体的字段列表代替“*”，不要返回用不到的任何字段。</p>
<p>20、尽量使用表变量来代替临时表。如果表变量包含大量数据，请注意索引非常有限（只有主键索引）。</p>
<p>21、避免频繁创建和删除临时表，以减少系统表资源的消耗。</p>
<p>22、临时表并不是不可使用，适当地使用它们可以使某些例程更有效，例如，当需要重复引用大型表或常用表中的某个数据集时。但是，对于一次性事件，最好使用导出表。</p>
<p>23、在新建临时表时，如果一次性插入数据量很大，那么可以使用 select into 代替 create table，避免造成大量 log ，以提高速度；如果数据量不大，为了缓和系统表的资源，应先create table，然后insert。</p>
<p>24、如果使用到了临时表，在存储过程的最后务必将所有的临时表显式删除，先 truncate table ，然后 drop table ，这样可以避免系统表的较长时间锁定。</p>
<p>25、尽量避免使用游标，因为游标的效率较差，如果游标操作的数据超过1万行，那么就应该考虑改写。</p>
<p>26、使用基于游标的方法或临时表方法之前，应先寻找基于集的解决方案来解决问题，基于集的方法通常更有效。</p>
<p>27、与临时表一样，游标并不是不可使用。对小型数据集使用 FAST_FORWARD 游标通常要优于其他逐行处理方法，尤其是在必须引用几个表才能获得所需的数据时。在结果集中包括“合计”的例程通常要比使用游标执行的速度快。如果开发时间允许，基于游标的方法和基于集的方法都可以尝试一下，看哪一种方法的效果更好。</p>
<p>28、在所有的存储过程和触发器的开始处设置 SET NOCOUNT ON ，在结束时设置 SET NOCOUNT OFF 。无需在执行存储过程和触发器的每个语句后向客户端发送 DONE_IN_PROC 消息。</p>
<p>29、尽量避免大事务操作，提高系统并发能力。</p>
<p>30、尽量避免向客户端返回大数据量，若数据量过大，应该考虑相应需求是否合理。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>PHP连接MySQL数据库的常用方式</title>
		<link>https://www.mudbest.com/php%e8%bf%9e%e6%8e%a5mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e5%b8%b8%e7%94%a8%e6%96%b9%e5%bc%8f/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Fri, 14 Sep 2012 08:22:09 +0000</pubDate>
				<category><![CDATA[Mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql_connect]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1166</guid>

					<description><![CDATA[平时写好的类库以及各种框架自带的MYSQL类都已经封装，只需要修改配置文件即可进行数据操作流程，可是平时当有人 ... <a title="PHP连接MySQL数据库的常用方式" class="read-more" href="https://www.mudbest.com/php%e8%bf%9e%e6%8e%a5mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e5%b8%b8%e7%94%a8%e6%96%b9%e5%bc%8f/" aria-label="阅读 PHP连接MySQL数据库的常用方式">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>平时写好的类库以及各种框架自带的MYSQL类都已经封装，只需要修改配置文件即可进行数据操作流程，可是平时当有人问起的时候，突然忘了，其实并不难，只是很少用，突然的一下子想不起来了，在这里整理出3种常用的PHP连接数据库的方式。</p>
<p><strong>@先声明连接信息的变量</strong><span id="more-1166"></span></p>
<pre class="brush: php; title: ; notranslate">
$username=your_name; //数据库连接用户名
$userpass=your_pass; //数据库连接密码
$dbhost=localhost; //数据库主机连接地址
$dbdatabase=your_database; //要访问的数据库名
</pre>
<p><strong>方法一：普通方法（面向过程）</strong></p>
<pre class="brush: php; title: ; notranslate">
//生成一个连接
 $db_connect=mysql_connect($dbhost,$username,$userpass) or die(&quot;Unable to connect to the MySQL!&quot;);
 //选择一个需要操作的数据库
 mysql_select_db($dbdatabase,$db_connect);
 //执行MySQL语句
 $result=mysql_query(&quot;SELECT id,name FROM user&quot;);
 //提取数据
 $row=mysql_fetch_row($result);
</pre>
<p>代码注释已经说明了一切。不过这里还需要做一些补充。</p>
<p>①在mysql_connect()、mysql_select_db()等函数之前使用<a href="http://php.net/manual/zh/language.operators.errorcontrol.php" title="错误控制运算符" target="_blank">@（错误控制运算符）</a>，可以忽略掉系统产生的错误信息，然后我们用die()来自定义错误信息；</p>
<p>②提取数据的时候，除了上面的mysql_fetch_row，常见的还有mysql_fetch_assoc和mysql_fetch_array，具体差别请查阅PHP Manual；</p>
<p>③对于mysql_query()函数的返回值，如果执行的语句有返回值（如SELECT、SHOW、DESCRIBE等），则返回相应数据（成功时）或FALSE（失败时）；如果执行的语句没有返回值（如DELETE、DROP、INSERT、UPDATE等），则返回TRUE（成功时）或FALSE（失败时）。</p>
<p><strong>方法二：面向对象方法</strong></p>
<pre class="brush: php; title: ; notranslate">
$db=new mysqli($dbhost,$username,$userpass,$dbdatabase);
 if(mysqli_connect_error()){
     echo 'Could not connect to database.';
     exit;
 }
 
 $result=$db-&gt;query(&quot;SELECT id,name FROM user&quot;);
 $row=$result-&gt;fetch_row();
</pre>
<p>这里用到的是mysqli，意思就是mysql的扩展，既可以通过面向过程的方式也可以通过面向对象的方式与数据库进行交互，唯一不同的是调用函数（对象方法）的方式不一样。</p>
<p><strong>方法三：PDO方法(可操作事务处理)</strong><br />
PDO其实是PHP Database Objects的缩写，中文即PHP数据库对象。它提供了一种统一的PHP与数据库交互的方法。</p>
<p>这是目前比较流行的一种连接数据库的方法。它的优势在于：只要正确提供数据源，余下对于数据库的基本操作都是一样的。也就是说，同一段代码既可以同MySQL交互，也可以和SQLite3交互，当然也可以和PostgreSQL进行交互，前提是你提供了正确的数据源。下面看一下连接MySQL的代码：</p>
<pre class="brush: php; title: ; notranslate">
$dsn='mysql:host='.$dbhost.';dbname='.$dbdatabase.';'
$dbh=new PDO($dsn,$username,$userpass);
</pre>
<p>如果是SQLite3，直接使用下面的代码：</p>
<pre class="brush: php; title: ; notranslate">
$dsn='sqlite3:&quot;C:\sqlite\user.db&quot;';
$dbh=new PDO($dsn);
</pre>
<p>如果是PostgreSQL，下面的代码能够对付：</p>
<pre class="brush: php; title: ; notranslate">
$dsn='pgsql:host='.$dbhost.' port=5432 dbname='.$dbdatabase.' user='.$username.' password='.$userpass;
$dbh=new PDO($dsn);
</pre>
<p>跟数据库成功建立连接之后，下面就只需要从数据库获取数据或插入更新数据，实例代码如下：</p>
<pre class="brush: php; title: ; notranslate">
$stmt=$dbh-&gt;query('SELECT id,name FROM user');
$row=$stmt-&gt;fetch();
</pre>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>找回mysql密码</title>
		<link>https://www.mudbest.com/%e6%89%be%e5%9b%9emysql%e5%af%86%e7%a0%81/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Mon, 16 Apr 2012 14:30:58 +0000</pubDate>
				<category><![CDATA[Mysql]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[找回密码]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1041</guid>

					<description><![CDATA[Windows下的实际操作如下 1.关闭正在运行的MySQL。 net stop mysql 2.打开DOS窗 ... <a title="找回mysql密码" class="read-more" href="https://www.mudbest.com/%e6%89%be%e5%9b%9emysql%e5%af%86%e7%a0%81/" aria-label="阅读 找回mysql密码">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>Windows下的实际操作如下</p>
<p>1.关闭正在运行的MySQL。 net stop mysql<br />
2.打开DOS窗口，转到mysql\bin目录。<br />
3.输入mysqld &#8211;skip-grant-tables回车。如果没有出现提示信息，那就对了<span id="more-1041"></span><br />
4.再开一个DOS窗口(因为刚才那个DOS窗口已经不能动了)，转到mysql\bin目录。<br />
5.输入mysql回车，如果成功，将出现MySQL提示符 ><br />
6. 连接权限数据库>use mysql; (>是本来就有的提示符,别忘了最后的分号)<br />
7.改密码：> update user set password=password(&#8220;520&#8243;) where user=&#8221;root&#8221;; (别忘了最后的分号)<br />
8.刷新权限(必须的步骤)>flush privileges;</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
