As developers, we often find ourselves working with MySQL databases on our local machines. While starting MySQL manually each time we boot up our system is manageable, it can be a tedious task. In this guide, we'll walk through the process of setting up MySQL to start automatically on macOS, saving you time and streamlining your workflow.
Before we begin, ensure you have:
If you've installed MySQL on macOS, you might have encountered issues starting it using the typical mysql.server start command. You may have seen errors like:
zsh: command not found: mysql.server
Or when trying to connect:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
These issues often arise because the MySQL commands aren't in your system's PATH, or the MySQL server isn't running.
The solution involves two parts:
To start MySQL manually, you can use the full path to the mysql.server script:
sudo /usr/local/mysql/support-files/mysql.server start
To stop MySQL:
sudo /usr/local/mysql/support-files/mysql.server stop
To make MySQL start automatically when your system boots, we'll create a launch agent. Here's how:
sudo nano /Library/LaunchDaemons/com.mysql.mysql.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>Label</key> <string>com.mysql.mysql</string> <key>ProgramArguments</key> <array> <string>/usr/local/mysql/support-files/mysql.server</string> <string>start</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist>
Save the file and exit the editor (in nano, press Ctrl X, then Y, then Enter).
Set the correct permissions for the plist file:
sudo chown root:wheel /Library/LaunchDaemons/com.mysql.mysql.plist sudo chmod 644 /Library/LaunchDaemons/com.mysql.mysql.plist
sudo launchctl load /Library/LaunchDaemons/com.mysql.mysql.plist
The launch agent we created tells macOS to run the MySQL start script when the system boots. Here's a breakdown of the plist file:
If you encounter issues:
By following this guide, you've now set up MySQL to start automatically on your macOS system. This setup will save you time and ensure your database is always ready when you need it. Remember, you can still manually start and stop MySQL using the full path commands if needed.
Happy coding!
The above is the detailed content of How to Set Up MySQL to Autostart on macOS: A Step-by-Step Guide for Developers. For more information, please follow other related articles on the PHP Chinese website!