LoginSignup
4
1

macOSでMariaDB

Last updated at Posted at 2023-10-19

MySQLからMairaDBに切り替えた際のメモ。

MySQLを同居させようとしたら色々エラーがでてしまったので、一旦同居は断念。
MySQLをアンインストールした状態で改めてMariaDBをインストールします。

Homebrewはインストール済みとして進めます。

1.MariaDBインストール

HomebrewでMariaDBをインストールします。

% brew install mariadb

あとは自動でインストールが始まっていくので暫し待機。

A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

MySQL is configured to only allow connections from localhost by default

To start mariadb now and restart at login:
  brew services start mariadb
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/mariadb/bin/mysqld_safe --datadir\=/usr/local/var/mysql

と出てきたらインストール完了。

2.MariaDBの起動

インストールしたら早速起動しましょう。

% brew services start mariadb

問題がなければ
==> Successfully started 'mariadb' (label: homebrew.mxcl.mariadb)
と表示されます。

psコマンドで確認。

% ps -ax | grep mysql
 7888 ??         0:00.03 /bin/sh /usr/local/opt/mariadb/bin/mysqld_safe --datadir=/usr/local/var/mysql
 7961 ??         0:00.30 /usr/local/opt/mariadb/bin/mariadbd --basedir=/usr/local/opt/mariadb --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/opt/mariadb/lib/plugin --log-error=/usr/local/var/mysql/MacBookPro.local.err --pid-file=MacBookPro.local.pid
 8281 ttys000    0:00.00 grep mysql

ちなみに、mariadbを停止するには
% brew services stop mariadb
再起動するには
%brew services restart mariadb
です。

3.セキュリティ設定

インストール直後はrootアカウントしかないのと、そのrootアカウントにはパスワードが設定されていないので、まずはrootにパスワードを設定しましょう

% sudo mysql_secure_installation

sudoで実行するのを忘れないように。
ちなみに、mysql_secure_installationは非推奨になっているようなので、可能な限りmariadb-secure-installationを使うようにしましょう。

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 

インストール直後なのでrootパスワードはありません。Enterを押します。

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] 

unix_socket認証に切り替えますか?という質問。
unix_socket認証というのは、OSのユーザ名とMariaDBのユーザ名が同じであればパスワードなしでログインできますよ、というもののよう。
同じこともあるかもしれないですが、同じでないことの方が多いような気もするので"n"を入力。

Change the root password? [Y/n] Y
New password: 
Re-enter new password: 

ルートのパスワードを設定(変更)するので”Y"。
設定するパスワードを入力して、確認のための再入力。

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] 

デフォルトでanonymousユーザが設定されているよ、という文言。
削除して問題ないので"Y"

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]

リモートでrootユーザでログインさせるか否か。
開発環境に合わせればよいと思いますが、今回は"n"。

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]

デフォルトで存在するtestという名のDBを削除するか否か。
いらないので"Y"

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]

設定変更を確実に反映させるかどうか、と。
反映させないわけがないのでもちろん"Y"

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

これでセキュリティの設定は完了です。

4. ユーザ作成

MariaDBを使うためのユーザを作成します。
まずはMariaDBにrootでログインします。

% mysql -u root -p
elcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 11.1.2-MariaDB Homebrew

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

現在の登録ユーザを確認
MariaDB [(none)]> select User, Host,Password from mysql.user;

+-------------+-----------+-------------------------------------------+
| User        | Host      | Password                                  |
+-------------+-----------+-------------------------------------------+
| mariadb.sys | localhost |                                           |
| root        | localhost | ***************************************** |
| OSuser      | localhost | invalid                                   |
| PUBLIC      |           |                                           |
+-------------+-----------+-------------------------------------------+
4 rows in set (0.003 sec)

おそらくこのような感じ。
OSuserは端末にログインしているユーザが登録されていると思います。
このユーザをパスワードだけ変更して使ってもいいですし、新しくユーザを作成してもOK。

MariaDB [(none)]> GRANT ALL ON *.* TO 'username'@'localhost' IDENTIFIED BY '********';
Query OK, 0 rows affected (0.022 sec)

usernameはOSuserをそのまま使うようであればOSuserを、新規ユーザを作成する場合は新しいユーザ名を入力します。'********'には任意のパスワードを。

+-------------+-----------+-------------------------------------------+
| User        | Host      | Password                                  |
+-------------+-----------+-------------------------------------------+
| mariadb.sys | localhost |                                           |
| root        | localhost | ***************************************** |
| OSuser      | localhost | ***************************************** |
| PUBLIC      |           |                                           |
| NewUser     | localhost | ***************************************** |
+-------------+-----------+-------------------------------------------+

これでユーザ作成は完了です。
MariaDBから抜けて作成したユーザでログインできればOKです。

MariaDB [(none)]> exit
Bye

% mysql -u username -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 11.1.2-MariaDB Homebrew

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

これでMariaDBを使う準備が整いました。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1