重复的 mysqld 进程导致错误 2002:无法通过套接字“/tmp/mysql.sock”连接到本地 MySQL 服务器

重复的 mysqld 进程导致错误 2002:无法通过套接字“/tmp/mysql.sock”连接到本地 MySQL 服务器

我使用的是 macOS 10.12,对此比较熟悉ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'。但我不认为这是常见的罪魁祸首之一。

套接字文件/tmp/mysql.sock丢失,但mysqld正在运行。实际上似乎有两个mysqld进程:

$ ps -axx | grep mysqld
  63 ??         0:00.02 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql
 109 ??         0:00.27 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid
 267 ??         0:00.20 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/localhost.err --pid-file=/usr/local/mysql/data/Jonathans-iMac.pid

我记得我前段时间安装和卸载了几个版本的 mysql。所以也许有一个旧实例还在,妨碍了我们?

但我找不到旧安装。我甚至无法终止第二个mysqld进程(我认为那是坏进程),否则它会立即重新启动。似乎mysqld_safe只是重新启动它。

有时重启整个系统就能奇迹般地让一切恢复正常。虽然仍然有两个mysqld进程,但至少/tmp/mysql.sock创建了一个进程,我可以正常访问数据库。其他时候,重启并没有帮助。

我已经寻找.plist可能负责额外mysqld过程的额外文件,但没有找到:

$ locate plist | grep mysql
/Library/LaunchDaemons/com.mysql.mysql.plist
/Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
/private/var/db/receipts/com.mysql.launchd.plist
/private/var/db/receipts/com.mysql.mysql.plist
/private/var/db/receipts/com.mysql.prefpane.plist

以下是/Library/LaunchDaemons/com.mysql.mysql.plist 的内容:

<!--?xml version="1.0" encoding="UTF-8"?-->
<plist version="1.0">
  <dict>
<key>KeepAlive</key>
<true />
<key>Label</key>
<string>com.mysql.mysqld</string>
<key>ProgramArguments</key>
<array>
  <string>/usr/local/mysql/bin/mysqld_safe</string>
  <string>--user=mysql</string>
</array>

和/Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>         
<string>com.oracle.oss.mysql.mysqld</string>
    <key>ProcessType</key>       <string>Interactive</string>
    <key>Disabled</key>          <false/>
    <key>RunAtLoad</key>         <true/>
    <key>KeepAlive</key>         <true/>
    <key>SessionCreate</key>     <true/>
    <key>LaunchOnlyOnce</key>    <false/>
    <key>UserName</key>          <string>_mysql</string>
    <key>GroupName</key>         <string>_mysql</string>
    <key>ExitTimeOut</key>       <integer>600</integer>
    <key>Program</key>           <string>/usr/local/mysql/bin/mysqld</string>
    <key>ProgramArguments</key>
        <array>
            <string>/usr/local/mysql/bin/mysqld</string>
            <string>--user=_mysql</string>
            <string>--basedir=/usr/local/mysql</string>
            <string>--datadir=/usr/local/mysql/data</string>
            <string>--plugin-dir=/usr/local/mysql/lib/plugin</string>
            <string>--log-error=/usr/local/mysql/data/mysqld.local.err</string>
            <string>--pid-file=/usr/local/mysql/data/mysqld.local.pid</string>
        </array>
    <key>WorkingDirectory</key>  <string>/usr/local/mysql</string>
</dict>
</plist>

关于如何识别和纠正问题的根源,您有什么想法吗?

答案1

正如您自己所注意到的,mysqld 似乎启动了两次。似乎在某个时候,您运行了两个不同版本的安装程序,它们安装在不同的位置,并且彼此之间没有相互识别。

删除其中一个

$ locate plist | grep mysql 
/Library/LaunchDaemons/com.mysql.mysql.plist 
/Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist

应该可以解决你的问题。

相关内容