ChatGPT解决这个技术问题 Extra ChatGPT

How to auto-load MySQL on startup on OS X Yosemite / El Capitan

After upgrading OS X my install of MySQL stopped loading on startup.

This walk-through on MySQL says:

"The Startup Item installation adds a variable MYSQLCOM=-YES- to the system configuration file /etc/hostconfig. If you want to disable the automatic startup of MySQL, change this variable to MYSQLCOM=-NO-."

So, I opened that file and it says:

# This file is going away 
AFPSERVER=-NO- 
AUTHSERVER=-NO-
TIMESYNC=-NO-
QTSSERVER=-NO-
MYSQLCOM=-YES-

I assume OSX dev's added the # This file is going away but I'm not certain.

If that is the case, what is the proper way to start MySQL on startup on OSX Yosemite?

Can you manually start MySql still?
I think this question might be better suited for Ask Different
This isn't a programming question per-se, but since Macs are primarily used as personal computers it stands to reason the vast majority of people trying to start MySQL on a Mac are developers. So this question is probably relevant for many users of SO.
This was answered here yesterday: MySQL does not start in OSX Yosemite 10.10
@dcc That question/answer is how to start MySQL after restarting (as the GUI tool won't do it). My question is how to make MySQL load on start-up of OSX.

J
Justin

This is what fixed it:

First, create a new file: /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>        
  </dict>
</plist>

Then update permissions and add it to launchctl:

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

Works! Thanks! The reason you need this is because there StartupItems have been removed from Yosemite. There is a note at the start of the page in Apple's docs explaining this: Startup items are a deprecated technology. Launching of daemons through this process may be removed or eliminated in a future release of OS X.
This worked for me as well, but on my system, the mysql user was actually _mysql. You can run dscacheutil -q user | grep mysql to see what the user should be on your system.
How are startup items a deprecated technology? What is the replacement for this feature?
Apple says in the depreciation notice, "use the launchd facility instead."
I had to change to --user=my_user_name to make it work.
Y
Yeonho

If you installed mysql via homebrew, you can have launchd start mysql at login by:

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

This has not worked for me on either Yosemite or El Capitan. However, @Justin's answer worked.
l
lensovet

The accepted answer did not work to auto-start my MySQL server (and in fact my Preferences Pane crashed System Preferences every time I tried to open it while it was active). I followed the instructions from the MySQL 5.6 handbook and it finally auto-starts again! Create the file /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist with the following content:

<?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>

And run the following commands after creating the file:

cd /Library/LaunchDaemons
sudo chown root:wheel com.oracle.oss.mysql.mysqld.plist 
sudo chmod o-w com.oracle.oss.mysql.mysqld.plist 
sudo launchctl load -F com.oracle.oss.mysql.mysqld.plist

C
Community

My Mac runs on El Capitan. MySQL installed via brew.

mysql.server status 

told me that I had some problems to solve:

ERROR! MySQL is not running, but PID file exists

Found homebrew.mxcl.mysql.plist file in /usr/local/Cellar/mysql/x.x.x/ directory and copied it to /Library/LaunchDaemons/

sudo cp homebrew.mxcl.mysql.plist /Library/LaunchDaemons/homebrew.mxcl.mysql.plist

Set all necessary permissions:

sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.mysql.plist
sudo chmod 644 /Library/LaunchDaemons/homebrew.mxcl.mysql.plist
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.mysql.plist

Used part of advices, described by Justin


P
Per Quested Aronsson

There is a bash script by MacMiniVault, which will do this for you - and install MySQL as well. There is also an article which describes the process.

It is suggested in the article to pipe the script straight into Terminal and run it, but as this has some serious security implications, it is a better idea to download and inspect the script before running it locally.

Note: This reply has been reworked in response to comments about the security implications of following the instructions in the aforementioned article. It is generally a bad idea to pipe a shell script from an unknown source directly to bash. Also, if you don't understand the script or trust the author, don't use it.


That's the way to fix it - just be sure to remove all existing mysql data before: community.jaspersoft.com/wiki/uninstall-mysql-mac-os-x
Piping a shell script from an unknown source directly to bash is most definitely not a good way to do anything.
@par: Yes, piping a shell script from an unknown source directly to bash is a bad idea. In this case, however, the shell script is open source and available for inspection at Github...
You're assuming the server that shows you the source code is the same one that serves it to your computer. On a large site such as github this is almost certainly not a valid assumption (i.e. a CDN is probably involved), so YES, you do want to treat the URL as an unknown source. Download the source, read it (better yet, calculate its SHA1 hash) and only after you've verified it install it.
I have modified the original reply taking the security concerns into consideration.