[忘備録] Alpine LinuxにMySQL導入

Alpine LinuxにMySQL導入

公式のwikiでも書かれていますがAplineではmysqlのパッケージをインストールするとMariaDBがインストールされる様です。こちらの方が速くてシンプルだと。今はそうかもしれませんし、実際は私もMaria入れようかな、と思っていたのでちょうどよかったのですが、パッケージ名は決してmysqlではないよーな。。。本当に入れたい時はどうするの??笑

Install

とはいえ、このままMariaDBをインストールします。

apk add mysql mysql-client

公式のwikiの通りインストール・設定していきます。

私がインストールしたVersionは

Server version: 10.6.9-MariaDB

です。他のVersionでは、以降に出てくるSQLのシンタックスが異なることがあります。その場合は調べてください。。。

最初に書いてある

The datadir located at /var/lib/mysql must be owned by the mysql user and group. The location of the datadir can be changed by editing the mariadb service file in /etc/init.d. The new location will also need to be set by adding datadir=<YOUR_DATADIR> in the [mysqld] section in a mariadb configuration file.

Normal initialization of mariadb can be done as follows:

  1. Initialize the main mysql database, and the data dir as standardized to /var/lib/mysql by running /etc/init.d/mariadb setup
  2. Start the main service. At this point there will be no root password set. /etc/init.d/mariadb start
  3. Secure the database by running mysql_secure_installation
  4. Setup permissions for managing others users and databases

↑をやります。

1の手順で、アクセス権限のエラーが出ました。以下で対処します。

sudo chown mysql:mysql /var/lib/mysql
sudo mkdir /var/tmp
sudo chown mysql:mysql /var/tmp

この後に1の手順を行います。

sudo /etc/init.d/mariadb setup

結果:

 * Creating a new MySQL database ...
Installing MariaDB/MySQL system tables in '/var/lib/mysql' ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


Two all-privilege accounts were created.
One is root@localhost, it has no password, but you need to
be system 'root' user to connect. Use, for example, sudo mysql
The second is mysql@localhost, it has no password either, but
you need to be the system 'mysql' user to connect.
After connecting you can set the password, if you would need to be
able to connect as any of these users with a password and without sudo

See the MariaDB Knowledgebase at https://mariadb.com/kb

You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/var/lib/mysql'

You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl

Please report any problems at https://mariadb.org/jira

The latest information about MariaDB is available at https://mariadb.org/.

Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/

うまく行った気がします。次、いきましょう。2の手順です。

❯ sudo /etc/init.d/mariadb start
 * Caching service dependencies ...                                                                               [ ok ]
 * Starting mariadb ...
221002 18:53:54 mysqld_safe Logging to syslog.
221002 18:53:54 mysqld_safe Starting mariadbd daemon with databases from /var/lib/mysql                           [ ok ]

問題なさそうです。3の手順に移ります。

sudo mysql_secure_installation

ここからは公式wikiではConfigurationセクションに移ります。

→ 以降が設定内容:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

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): 
OK, successfully used password, moving on...

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] 
→ Y

Enabled successfully!
Reloading privilege tables..
 ... Success!


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

Change the root password? [Y/n]
→ Y

Remove anonymous users? [Y/n]
→ Y
 ... Success!

Disallow root login remotely? [Y/n]
→ Y
 ... Success!
 
Remove test database and access to it? [Y/n]
→ Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

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

Reload privilege tables now? [Y/n] 
→ Y
 ... Success!

Cleaning up...

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

Thanks for using MariaDB!

これでセットアップ完了です。↑の設定の中でも

  • Remove anonymous users? [Y/n]
  • Disallow root login remotely? [Y/n]

上記は両方ともYにした方がいいですね。

Alpine Linuxにサービスとして自動起動する様に設定します。

sudo rc-update add mariadb default
# ついでにすぐにMariaDBを立ち上げときましょう↓
sudo rc-service mariadb restart

ここまででインストール作業は終了しました。

実際にMariaDBに接続できるかどうか、試してみましょう。

クライアントは最初のインストールコマンドで導入した通り、mysql-clientを利用します。

mysql -uroot -h localhost -p

パスワードを聞かれると思いますで入力します。初めての起動の場合、パスワードがないのでそのままリターンすれば入れます。

Databaseとユーザー追加

各種サービス向けにデータベース(DB)を作って、そのDBにアクセスできる様にユーザーを追加することになりますね、追加しましょう。

今回、私はPiwigoで必要なのでPiwigo用のDB&ユーザーを追加することにします。適宜、読み替えてください。

データベースの作成

CREATE DATABASE `piwigo_db`;

シングルクォーテーション(')ではなく、バッククォーテーション(`)です、データベース名を囲んでいるのは。

ユーザーの作成

CREATE USER piwigo_db_user@localhost IDENTIFIED BY 'password';

piwigo_db_user,passwordは適宜入力しなおしてください。

ユーザーに作ったデータベースにアクセスできる権限をつける

GRANT ALL ON `piwigo_db`.* TO 'piwigo_db_user'@localhost;
FLUSH PRIVILEGES;

ここもデータベースの名前はシングルクォーテーション(')ではなく、バッククォーテーション(`)です

確認

ここまででデータベースの追加、ユーザーの作成が終了。あとは利用する側のサービス側からの操作です。 一応作成したユーザーから作ったDBにアクセスできるかどうか確認してみましょう。

mysql -u piwigo_db_user -p -h localhost
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| piwigo_db          |
+--------------------+
2 rows in set (0.001 sec)

MariaDB [(none)]> use piwigo_db;
Database changed
MariaDB [piwigo_db]> show tables;
Empty set (0.001 sec)

MariaDB [piwigo_db]>

問題なく参照できました!

 

コメント

このブログの人気の投稿

My 3rd DIY Keyboard - Levinson

My 2nd Iris Keyboard

My first - Iris Keyboard -