オッサンの屋根裏実験室

Main navigation

  • ホーム
  • 参考リンク集

パンくず

  • ホーム
  • ArchlinuxにLAMP環境を構築する(2)-MariaDBのセットアップ

ArchlinuxにLAMP環境を構築する(2)-MariaDBのセットアップ

By Ossan | 木 12月 10, 2020

ArchlinuxにMariaDBをインストールし、PHPを使ってアクセスできるようにします。

ArchWikiのMariaDBのページ

MariaDBのインストール

MariaDBパッケージをインストールします。

$ sudo pacman -S mariadb

パッケージのインストール中に以下のメッセージが表示されます。

:: You need to initialize the MariaDB data directory prior to starting
   the service. This can be done with mariadb-install-db command. e.g.: 
   mariadb-install-db --user=mysql --base-dir=/usr --data-dir=/user/lib/mysql

これはMariaDBを起動する前に、データベースが置かれるディレクトリを初期化する必要がありますというメッセージです。

メッセージではmariadb-install-db(ハイフンになっている)ですが、実際のコマンドは以下になります(アンダースコア)。

$ sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

以下のメッセージが表示されます。

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

このメッセージは重要で、初めてMariaDBにログインするときはホストマシンのroot権限で行う必要があるということ、ログイン後にMariaDBのroot@localhostアカウントにパスワードを設定する必要があると言っています。

MariaDBを起動します。

$ sudo systemctl start mariadb.service

セキュリティーの確保に望ましい設定を行う

データベースのセキュリティを確保するために以下のコマンドを実行します。

$ sudo mysql_secure_installation
中略
In order to log into MariaDB to securee 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@localhostアカウントにパスワードが設定されていないので、Enterキーを押します。

次にroot@localhostアカウントにパスワードを設定するのか、それともUnixソケットを使った認証に切り替えるかどうか尋ねられます。

Switch to unix_socket authetication [Y/n] 

n を入力して、パスワードの設定に移ります。

Change the root password? [Y/n]

Y を入力するか、Enterキーを押してパスワードを設定します。忘れないように控えておきましょう。

次にanonymousユーザーの削除を行うか尋ねられますので、Y を入力するか、Enterキーを押して削除を行います。

Remove anonymous users? [Y/n]

rootアカウントでのリモートログインを許可しないようにするか尋ねられますので、Y を入力するか、Enterキーを押して許可しない設定にします。

Disallow root login remotely? [Y/n]

test という名前のデータベース(テスト用)を削除するか尋ねられますので、Y と入力するか、Enterキーを押して削除します。

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

最後に権限テーブルを再読み込みしてここまでの設定を反映させますので、Y と入力するか、Enterキーを押して終了です。

Reload privilege tables now? [Y/n]

一般ユーザー権限でmysqlコマンドを使用してログインできるか確認します。

$ mysql -u root -p
Enter password: 

先ほど控えておいたrootパスワードでログインできればOKです。

次にネットワークからアクセスできないように設定します。

$ sudo vi /etc/my.conf.d/server.cnf

[変更前] 
# this is read by the standalone daemon and embedded servers
[server] 

# this is only for the mysqld standalone daemon
[mysqld] 

[変更後]
# this is read by the standalone daemon and embedded servers
[server] 
skip-networking=1

# this is only for the mysqld standalone daemon
[mysqld] 
skip-networking=1

データベースのデフォルト文字セットの設定

データベースで使う文字セットをUTF8MB4に変更します。まず、現在のデータベースをバックアップします。

$ mysqldump -u root -p --all-databases > all-databases-backup.sql

設定ファイルを編集します。

$ sudo vi /etc/my.cnf.d/server.cnf

[変更前]
# this is read by the standalone daemon and embedded servers
[server] 
skip-networking=1

# this is only for the mysqld standalone daemon
[mysqld] 
skip-networking=1

[変更後]
# this is read by the standalone daemon and embedded servers
[server] 
skip-networking=1
collation_server=utf8mb4_unicode_ci
character_set_server=utf8mb4

# this is only for the mysqld standalone daemon
[mysqld] 
skip-networking=1
collation_server=utf8mb4_unicode_ci
character_set_server=utf8mb4
$ sudo vi /etc/my.cnf.d/client.cnf

[変更前]
[client]

# This group is not read by mysql client library.

[変更後]
[client] 
default-character-set=utf8mb4

# This group is not read by mysql client library.
$ suco vi /etc/my.cnf.d/mysql-clients.cnf

[変更前]
[mysql]

[mysql_upgrade]

[変更後]
[mysql]
default-character-set=utf8mb4

[mysql_upgrade]

MariaDBを再起動します。またホスト起動時に自動起動するようにします。

$ sudo systemctl restart mariadb.service
$ sudo systemctl enable mariadb.service

PHPを使ってMariaDBにアクセスできるようにする

PHPの設定ファイルを編集して、MariaDBにアクセスするために必要なモジュールを有効にします。

$ sudo vi /etc/php/php.ini

[変更前]
;extension=ldap
;extension=mysqli
;extension=odbc
;zend_extension=opcache
;extension=pdo_dblib
;extension=pdo_mysql
;extension=pdo_pgsql

[変更後]
;extension=ldap
extension=mysqli
;extension=odbc
;zend_extension=opcache
;extension=pdo_dblib
extension=pdo_mysql
;extension=pdo_pgsql

Apacheを再起動します。

MariaDB
PHP

コメント

最近の投稿

SSHを使ってリモートのmysql(MariaDB)データベースをバックアップ・リストアする
ArchlinuxにLAMP環境を構築する(2)-MariaDBのセットアップ
ArchlinuxにLAMP環境を構築する(1) - ApacheとPHPのセットアップ
ページ送り
  • 前ページ ‹‹
  • 2 ページ
Powered by Drupal

Copyright © 2025 オタルラボ - All rights reserved

Developed & Designed by オタルラボ