<p><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">这种情况大多数是因为你的mysql是使用rpm方式安装的,它会自动寻找 /var/lib/mysql/mysql.sock 这个文件,</span><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">通过unix socket登录mysql。</span><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">常见解决办法如下:</span></p><p><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">1、</span><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">创建/修改文件 /etc/my.cnf,至少增加/修改一行</span></p><p><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">[mysql]</span><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">[client]</span></p><p><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">socket = /tmp/mysql.sock</span></p><p><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">#在这里写上你的mysql.sock的正确位置,通常不是在 /tmp/ 下就是在 /var/lib/mysql/ 下</span></p><p><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">2、</span><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">指定IP地址,使用tcp方式连接mysql,而不使用本地sock方式</span></p><p><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">#mysql -h127.0.0.1 -uuser -ppassword</span></p><p><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">3、</span><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">为 mysql.sock 加个连接,比如说实际的mysql.sock在 /tmp/ 下,则</span></p><p><span style="font-family: monospace; white-space: pre-wrap;font-size:16px; ">#ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock即可</span></p>