在Mac OS X Yosemite / El Capitan上自动启动MySQL服务器

我想在启动时自动启动MySQL服务器。 这在小牛队是可能的,但似乎没有在优胜美地工作。

编辑 :似乎这与El Capitan一起工​​作

在这里输入图像说明

@dcc非常接近。 这就是MySQL如何在优胜美地自动启动:

/Library/LaunchDaemons

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <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> </dict> </plist> 

另外,我根据这个答案改变了权限

 sudo chown root:wheel /Library/LaunchDaemons/com.mysql.mysql.plist sudo chmod 644 /Library/LaunchDaemons/com.mysql.mysql.plist 

最后我运行这个命令

 sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist 

如果您有任何补充,请分享下面!

我遵循@revaxarts指示并试图执行该命令

 sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist 

被给出了错误:

/Library/LaunchDaemons/com.mysql.mysql.plist:无效的属性列表

我的头脑一分钟后,我发现删除顶部的DOCTYPE DTD声明使得错误消失,重启后,mySQL服务器确实正在运行。

所以,我的XML看起来像这样:

 <?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> </dict> </plist> 

如果你用自制软件安装了mysql ,你可以通过inputbrew info mysql来获得如何自动启动它的说明。

例如,我的机器上的输出是:

 To have launchd start mysql at login: ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents Then to load mysql now: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist 

创build/Library/LaunchDaemons/com.mysql.mysql.plist并用下面的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> </true> </dict> </plist> 

然后加载新创build的plist文件

 sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist 

本文帮助我解决无效错误的问题。 我在下面使用更正的plist。

如何知道plist文件的哪一行是不正确的

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList- 1.0.dtd"> <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> </dict> </plist> 

没有其他提供的答案工作自动启动我的MySQL服务器。 我按照MySQL 5.6手册中的说明操作,最后再次自动启动! 使用以下内容创build文件/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> <string>--port=3306</string> </array> <key>WorkingDirectory</key> <string>/usr/local/mysql</string> </dict> </plist> 

创build文件后运行以下命令:

 cd /Library/LaunchDaemons sudo launchctl load -F com.oracle.oss.mysql.mysqld.plist 

当我使用先前答案中提到的plist时,我将用户更改为我的系统的_mysql,但是MySQL偏好窗格中的“停止MySQL服务器”button不再有效。 按下停止button后, KeepAlive键将立即再次启动进程。 我使用了RunAtLoadRunAtLoad在重新启动时开始,但是允许窗格中的button继续工作。

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>RunAtLoad</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> </dict> </plist> 

然后,如在其他答案,跑了:

 sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist 

现在,MySQL重新启动,但系统偏好设置中的MySQL窗格仍然有效。 我正在运行El Capitan,10.11.2