macOS MontereyでMariaDBを自動起動する方法
macOSでMariaDBを自動起動する方法
MariaDBはMySQLと互換性のあるオープンソースのデータベース管理システムです。macOSでMariaDBを自動起動するには、いくつかの方法があります。
方法
Homebrew サービスを使う
Homebrew のインストール
Homebrew は macOS 用のパッケージマネージャーです。まだインストールしていない場合は、以下のコマンドを実行してインストールします。
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
MariaDB のインストール
以下のコマンドを実行して MariaDB をインストールします。
brew install mariadb
MariaDB サービスの起動
brew services start mariadb
自動起動の設定
brew services start mariadb --auto
launchd を使う
設定ファイルの作成
以下の内容を ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist
という名前のファイルに保存します。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>homebrew.mxcl.mariadb</string>
<key>ProgramArguments</key>
<array>
<string>/opt/homebrew/opt/mariadb/bin/mysqld_safe</string>
<string>--datadir=/opt/homebrew/var/mysql</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>WorkingDirectory</key>
<string>/opt/homebrew/opt/mariadb</string>
</dict>
</plist>
権限の設定
以下のコマンドを実行して、作成したファイルの権限を設定します。
chmod 644 ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist
サービスの起動
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist
上記の launchctl load
コマンドは、手動で実行する必要があります。MariaDB サービスを自動的に起動するには、以下のコマンドを実行します。
launchctl enable system/homebrew.mxcl.mariadb
その他の方法
上記以外にも、systemd や cron を使って MariaDB を自動起動する方法もあります。詳細は以下のサイトを参照してください。
注意事項
- 上記の手順は、macOS Monterey 12.4 で動作確認しています。他のバージョンの macOS では、手順が異なる場合があります。
- 自動起動を設定する前に、MariaDB サービスが正しく起動することを確認してください。
- MariaDB サービスの設定を変更する場合は、サービスを停止してから変更してください。
# Homebrew のインストール
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# MariaDB のインストール
brew install mariadb
# MariaDB サービスの起動
brew services start mariadb
# 自動起動の設定
brew services start mariadb --auto
launchd を使う
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>homebrew.mxcl.mariadb</string>
<key>ProgramArguments</key>
<array>
<string>/opt/homebrew/opt/mariadb/bin/mysqld_safe</string>
<string>--datadir=/opt/homebrew/var/mysql</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>WorkingDirectory</key>
<string>/opt/homebrew/opt/mariadb</string>
</dict>
</plist>
chmod 644 ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist
launchctl enable system/homebrew.mxcl.mariadb
MariaDBを自動起動するその他の方法
systemd は Linux で広く使われているサービス管理ツールですが、macOS でも利用可能です。systemd を使って MariaDB を自動起動するには、以下の手順が必要です。
- systemd ユニットファイルの作成
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>homebrew.mxcl.mariadb</string>
<key>ExecStart</key>
<string>/opt/homebrew/opt/mariadb/bin/mysqld_safe</string>
<key>ExecStartPre</key>
<array>
<string>/opt/homebrew/opt/mariadb/bin/mysqld_pre_systemd</string>
</array>
<key>Type</key>
<string>forking</string>
<key>User</key>
<string>_mysql</string>
<key>Group</key>
<string>mysql</string>
<key>WorkingDir</key>
<string>/opt/homebrew/opt/mariadb</string>
<key>PIDFile</key>
<string>/opt/homebrew/var/mysql/mysqld.pid</string>
<key>Environment</key>
<dict>
<key>PATH</key>
<string>/opt/homebrew/opt/mariadb/bin:/usr/local/bin:/usr/bin:/bin</string>
</dict>
</dict>
</plist>
sudo chmod 644 /Library/LaunchDaemons/homebrew.mxcl.mariadb.plist
sudo launchctl start homebrew.mxcl.mariadb
sudo launchctl enable homebrew.mxcl.mariadb
cron は Unix 系システムで定期的にジョブを実行するツールです。cron を使って MariaDB を自動起動するには、以下の手順が必要です。
- cron 設定ファイルの編集
以下のコマンドを実行して cron 設定ファイルを開きます。
crontab -e
- 設定の追加
以下の設定を cron 設定ファイルに追加します。
@reboot /opt/homebrew/opt/mariadb/bin/mysqld_safe
- 設定の保存
ファイルを保存して、エディタを閉じます。
手動で起動する
上記の方法で自動起動を設定しなくても、手動で MariaDB を起動することができます。
/opt/homebrew/opt/mariadb/bin/mysqld_safe
MariaDB を自動起動するには、いくつかの方法があります。それぞれの方法にはメリットとデメリットがありますので、自分の環境に合った方法を選択してください。
macos mariadb