4

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?

2 Answers 2

5

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
4

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
3
  • You should not add/alter files in /usr/bin as part of the OS and Apple can add or remove them and in El Capitan they have made it difficult to do
    – mmmmmm
    Oct 9, 2015 at 13:22
  • what alternative do you propose?
    – Robbo_UK
    Oct 9, 2015 at 13:34
  • The other answer - or link to /usr/local/bin
    – mmmmmm
    Oct 9, 2015 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .