Starting and stopping mysql server. Shorten and make automatic

Solution 1:

Just create an alias in your ~/.bash_profile or ~/.profile file.

Check with ls -la0 ~/ if one of the files already exist, else create one with:

touch ~/.bash_profile

After opening .bash_profile with nano ~/.bash_profile add the lines:

alias iwanttostartmysqlwithareallyshortcommand='sudo /usr/local/mysql/support-files/mysql.server start'
alias iwanttostopmysqlwithareallyshortcommand='sudo /usr/local/mysql/support-files/mysql.server stop'

write the file to disk with ctrlO and exit nano with ctrlX.

Then enter:

source .bash_profile

In the future you just have to enter

iwanttostartmysqlwithareallyshortcommand

or

iwanttostopmysqlwithareallyshortcommand

to start or stop mysql after entering your password.

A shorter alias like iwanttostartmysqlnow works also. Even really, really short ones are possible like stm (= start mysql) or spm (= stop mysql). They mustn't collide with other aliases or valid commands though. The shortest I have found - and easy to remember - are 1 to start and 0 to stop mysql. ;-)


To answer your second question:

If you have installed the latest MySQL from Oracle a launch daemon should have been installed in /Library/LaunchDaemons already.

If you have installed another mysql package (e.g. homebrew) you may use the example here and adapt it.

Slightly modified example:

<?xml version="1.0" encoding="UTF-8"?>
<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/support-files/mysql.server start</string>
    </array>
  </dict>
</plist>

Save the XML as a file named /Library/LaunchDaemons/com.mysql.mysql.plist

Adjust the file permissions using the Apple recommended owner "root", owning group "wheel", and file permissions "644"

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

Enable this new MySQL service with:

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

Solution 2:

I like the alias answer. This is another route I found whilst looking.

sudo ln -s /usr/local/mysql/support-files/mysql.server /usr/bin/mysql.server

can now stop and start with

sudo mysql.server start
sudo mysql.server stop