Starting and stopping thesql server. Shorten and make automatic

command lineMySQLservicesshortcut

Currently I start and stop mysql with the following command

sudo /usr/local/mysql/support-files/mysql.server start
sudo /usr/local/mysql/support-files/mysql.server stop

It is quite a long command to remember. How can I set it up so that I can start and stop it with a single command. Something like.

sudo mysql-server start

I am thinking I would need to crete a symbolic link somewhere for this to work? Also one last option; how can I make it so that it runs on bootup by default?

Best Answer

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