MySQL サーバー構築

MariaDB をインストール
root@falcon21:~# dnf -y install mariadb-server
インストール済み:
mariadb-3:10.11.11-1.el10.x86_64         mariadb-backup-3:10.11.11-1.el10.x86_64
mariadb-client-utils-3:10.11.11-1.el10.x86_64  mariadb-common-3:10.11.11-1.el10.noarch
mariadb-connector-c-3.4.4-1.el10.x86_64    mariadb-connector-c-config-3.4.4-1.el10.noarch
mariadb-errmsg-3:10.11.11-1.el10.noarch    mariadb-gssapi-server-3:10.11.11-1.el10.x86_64
mariadb-server-3:10.11.11-1.el10.x86_64     mariadb-server-utils-3:10.11.11-1.el10.x86_64
mysql-selinux-1.0.14-1.el10_0.noarch perl-DBD-MariaDB-1.23-10.el10.x86_64

完了しました!

文字コード設定ファイルを新規作成 root@falcon21:~# vi /etc/my.cnf.d/charset.cnf
[mysqld]
character-set-server = utf8mb4

[client]
default-character-set = utf8mb4


—————-
MariaDBサーバー設定ファイル編集
[root@falcon21 ~]# vi /etc/my.cnf.d/mariadb-server.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
character-set-server = utf8mb4  ← 追加(MariaDBサーバーの文字コードをutf8mb4にする


—————-
MariaDBクライアント設定ファイル編集
[root@falcon21 ~]# vi /etc/my.cnf.d/client.cnf
[client]
default-character-set=utf8mb4


————

mariadb 自動起動設定
root@falcon21:~# systemctl enable –now mariadb
Created symlink ‘/etc/systemd/system/mysql.service’ → ‘/usr/lib/systemd/system/mariadb.service’.
Created symlink ‘/etc/systemd/system/mysqld.service’ → ‘/usr/lib/systemd/system/mariadb.service’.
Created symlink ‘/etc/systemd/system/multi-user.target.wants/mariadb.service’ → ‘/usr/lib/systemd/system/mariadb.service’.


[root@falcon21 ~]# systemctl restart mariadb
● mariadb.service – MariaDB 10.11 database server
    Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: disabled)
    Active: active (running) since Mon 2025-11-17 18:33:11 JST; 1min 5s ago
Invocation: 76833421f85b473191edf67ee76f5c8f
     Docs: man:mariadbd(8)
         https://mariadb.com/kb/en/library/systemd/
    Process: 8382 ExecStartPre=/usr/libexec/mariadb-check-socket (code=exited, status=0/SUCCESS)
    Process: 8404 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/S>
    Process: 8451 ExecStartPost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/SUCCESS)
   Main PID: 8440 (mariadbd)
     Status: “Taking your SQL requests now…”
      Tasks: 8 (limit: 47782)
     Memory: 199.1M (peak: 201.1M)
        CPU: 152ms
   CGroup: /system.slice/mariadb.service
        └─8440 /usr/libexec/mariadbd –basedir=/usr –skip-grant-tables

11月 17 18:33:11 falcon21.space systemd[1]: Starting mariadb.service – MariaDB 10.11 database server…
11月 17 18:33:11 falcon21.space mariadb-prepare-db-dir[8404]: Database MariaDB is probably initialized in />
11月 17 18:33:11 falcon21.space mariadb-prepare-db-dir[8404]: If this is not the case, make sure the /var/l>
11月 17 18:33:11 falcon21.space (mariadbd)[8440]: mariadb.service: Referenced but unset environment variabl>
11月 17 18:33:11 falcon21.space systemd[1]: Started mariadb.service – MariaDB 10.11 database server.


—————-
firewall 設定
root@falcon21:~# firewall-cmd –add-service=mysql
success

root@falcon21:~# firewall-cmd –runtime-to-permanent
success

MariaDB 初期設定
root@falcon21:~# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
 注意:本番環境のすべてのMariaDBサーバーでは、このスクリプトの全部分を実行することをお勧めします。各ステップをよくお読みください。

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.
 MariaDBにログインしてセキュリティを確保するには、現在のrootユーザーのパスワードが必要です。MariaDBをインストールしたばかりで、まだrootパスワードを設定していない場合は、ここでEnterキーを押してください。


Enter current password for root (enter for none):・・・ root パスワード
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.
root パスワードを設定するか、unix_socket を使用すると、適切な認証なしに MariaDB の root ユーザーにログインできなくなります。
You already have your root account protected, so you can safely answer ‘n’.
ルートアカウントは既に保護されているため、「n」と安全に答えることができます。


Switch to unix_socket authentication [Y/n] ・・・n をenter
… skipping.

You already have your root account protected, so you can safely answer ‘n’.
ルートアカウントは既に保護されているため、「n」と安全に答えることができます。


Change the root password? [Y/n] ・・・n をenter
… skipping.

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.
デフォルトでは、MariaDBインストールには匿名ユーザーが含まれており、ユーザーアカウントを作成しなくても誰でもMariaDBにログインできます。
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] n をenter匿名ユーザーを削除しますか?
… skipping.

Normally, root should only be allowed to connect from ‘localhost’. This
ensures that someone cannot guess at the root password from the network.
通常、root は「localhost」からのみ接続を許可する必要があります。これにより、ネットワークから root パスワードを推測されることがなくなります。

Disallow root login remotely? [Y/n] 空enter
DELETE FROM mysql.global_priv WHERE User=’root’ AND Host NOT IN (‘localhost’, ‘127.0.0.1’, ‘::1’)
————–

ERROR 1142 (42000) at line 1: DELETE command denied to user ‘root’@’localhost’ for table `mysql`.`global_priv`
… Failed!
1行目のエラー 1142 (42000): テーブル `mysql`.`global_priv` に対する DELETE コマンドがユーザー ‘root’@’localhost’ に拒否されました… 失敗しました!

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.
MariaDBにはデフォルトで、「test」という名前のデータベースが付属しており、誰でもアクセスできます。これもテスト専用であるため、本番環境に移行する前に削除する必要があります。


Remove test database and access to it? [Y/n] 空enter
– Dropping test database…
————–
DROP DATABASE IF EXISTS test
————–

ERROR 1044 (42000) at line 1: Access denied for user ‘root’@’localhost’ to database ‘test’
… Failed! Not critical, keep moving…
– Removing privileges on test database…
————–
DELETE FROM mysql.db WHERE Db=’test’ OR Db=’test\_%’
————–

ERROR 1142 (42000) at line 1: DELETE command denied to user ‘root’@’localhost’ for table `mysql`.`db`
… Failed! Not critical, keep moving…

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
権限テーブルを再ロードすると、これまでに加えられたすべての変更がすぐに有効になります。


Reload privilege tables now? [Y/n] yをenter
FLUSH PRIVILEGES
————–

ERROR 1227 (42000) at line 1: Access denied; you need (at least one of) the RELOAD privilege(s) for this operation
… Failed!

Cleaning up…

All done! If you’ve completed all of the above steps, your MariaDB
installation should now be secure.
完了です!上記の手順をすべて完了すると、MariaDBのインストールは安全になります。

Thanks for using MariaDB!
[root@falcon21 ~]#

root@falcon21 ~]# mysql -u root -p
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 10.11.11-MariaDB MariaDB Server

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)]> exit
Bye
[root@falcon21 ~]#

—————-

MySQLの起動オプションで、MYSQLD_OPTS=”–skip-grant-tables” を設定すると
パスワード認証を無効化することが出来ます。


rootパスワード無しでログインする設定
mysqldを停止
[root@falcon21 ~]# systemctl stop mysqld


MYSQLD_OPTSでパスワード認証を無効化
[root@falcon21 ~]# systemctl set-environment MYSQLD_OPTS=”–skip-grant-tables”


mysqldを再起動
[root@falcon21 ~]# systemctl restart mysqld


mysqlに入る
[root@falcon21 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.11.11-MariaDB MariaDB Server

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)]> exit
Bye
[root@falcon21 ~]#

———————

MariaDB 確認       
[root@falcon21 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.11.11-MariaDB MariaDB Server

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)]> show grants for root@localhost; MariaDB [(none)]> select user,host,password from mysql.user;

+————-+———–+———-+
| User | Host | Password |
+————-+———–+———-+
| mariadb.sys | localhost | |
| root | localhost | invalid |
| mysql | localhost | invalid |
+————-+———–+———-+
3 rows in set (0.003 sec)
MariaDB [(none)]> exit
Bye
[root@falcon21 ~]#

———————

MySQLユーザー追加設定
[root@falcon21 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.11.11-MariaDB MariaDB Server

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

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


# root | localhost | invalid |なので、一旦、root を削除、
# root ユーザー削除・再設定
MariaDB [(none)]> drop user ‘root’@’localhost’;
ERROR 1290 (HY000): The MariaDB server is running with the –skip-grant-tables option so it cannot execute this statement MariaDB [(none)]>

MariaDB [(none)]> create user ‘root’@’localhost’ identified by ‘1234567890’;
ERROR 1290 (HY000): The MariaDB server is running with the –skip-grant-tables option so it cannot execute this statement

MariaDB [(none)]> exit
Bye


—————–

–skip-grant-tables option を設定すると、ユーザー設定でエラーが発生するので、元に戻す。
元に戻しても root で、mysq に入る事はできる。

root@falcon21 ~]# systemctl stop mysqld
[root@falcon21 ~]# systemctl set-environment MYSQLD_OPTS=””
[root@falcon21 ~]# systemctl start mysqld

—————-

ユーザー再設定
rootに管理者権限を設定

MariaDB [(none)]> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘1234567890’;
Query OK, 0 rows affected (0.015 sec)

ユーザー追加
MariaDB [(none)]> create user ‘xxxxxx’@’localhost’ identified by ‘0987654321’;
Query OK, 0 rows affected (0.018 sec)