1

I currently use Valet to develop sites, and many of those sites need SQL databases. To host SQL databases, I run a mariaDB server from the Terminal. In order to start the MySQL server so that I can connect to the databases, I run the command: sudo mysql.server start I want the server to start on login, so normally I would just have this command execute on login, but it requires the sudo command. What do I change and how do I change it so that I do not need the sudo part of the command and I can just start the server on login?

  • 1
    See if this Q&A helps. It's not a duplicate question but it has info that might assist you. – fsb Jan 13 '17 at 17:31
2

The best way to do this is to create a launchd .plist, copy it to ~/Library/LaunchAgents and let the system handle it for you.

Sample .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.user.startMySQL</string>
        <key>ProgramArguments</key>
        <array>
          <string>mysql.server</string>
          <string>start</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
</dict>
</plist>

I prefer to use the file naming convention com.user.<name of function>.plist when naming them so I can easily identify what is loaded and what is not.

Next, you need to load this .plist as root so it runs as root

$ sudo launchctl load com.user.startMySQL.plist

To verify that it has loaded you can issue the command sudo launchctl list and the command mysql to initiate a connection to the MySQL service.

Please note that this is just a simple example - you may wish to create a script to check to see if mysqld is already running before launching it.

  • but it sounds like I will have to run this command every time I want to use the server, so I dont see how this is better than what I do now. actually I was just looking for which permissions or owner to change to make this command runnable by me without sudo, so that I can then just execute this command on login somehow. – brothman Jan 15 '17 at 3:28
  • You only have to do this once. It will launch MySQL automatically every time you log in – Allan Jan 15 '17 at 5:06
  • oh ok thanks Tell me if understand this correctly: 'launchd' is a daemon that starts at launch and runs every job in 'launchctl'. In order to add a job to launchctl, define it in a .plist file and copy that file to ~/Library/LaunchAgents, then load the plist into launchctl with$ launchctl load com.user.startMySQL.plist and the reason a path does not have to be given in the command is that a file name is sufficient as long as it is a file in the 'LaunchAgents' directory because launchctl looks in that directory. Once loaded into launchctl, launchd will run the new job on login. – brothman Jan 18 '17 at 3:20
  • That's basically correct. I have written about launchd in more detail in this answer Personally, I wourld start your server on boot rather than on login, but whatever suits you best... – Allan Jan 18 '17 at 13:42

You must log in to answer this question.

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