[記録][Ubuntu]MariaDB から MySQL へ変更

 [XServer VPS](https://vps.xserver.ne.jp) を２つ使っていて、片方は OS のみの Ubuntu 22.04 のイメージも使っていて、FileMaker Server で使っています。 もう一方は、イメージ LEMP 。これは、Ubuntu、nginx、MariaDB、PHP という構成になっています。

 で、最近、MariaDB で色々、都合の悪いことが出てきたので、MySQL 8 へスイッチしました。
 その記録です。

## XServer VPS の VPSパネルでバックアップ
`電源操作 > シャットダウン` でイメージ保存できる状態にして、`サーバー > イメージ保存` で `イメージ保存` でバックアップ。
　これが終わったら、サーバへログインし、データを別途、

 ## OS バージョン
 ```
 # lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.5 LTS
Release:	22.04
Codename:	jammy
```

## MariaDB 確認
```
# mysql -u root -p

MariaDB [(none)]> SELECT VERSION();
+----------------------------------+
| VERSION()                        |
+----------------------------------+
| 10.6.18-MariaDB-0ubuntu0.22.04.1 |
+----------------------------------+
1 row in set (0.000 sec)

MariaDB [(none)]> SELECT @@SQL_MODE;
+-------------------------------------------------------------------------------------------+
| @@SQL_MODE                                                                                |
+-------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)

MariaDB [(none)]> exit
```

## MariaDB の停止と削除
```
sudo systemctl stop mariadb
sudo apt purge mariadb-server
sudo apt purge mariadb-client
sudo apt purge mariadb-common
sudo rm -rf /var/lib/mysql/
sudo rm -rf /etc/mysql/
```

## 確認して、MySQL インストール
```
sudo apt update
sudo apt search mysql

sudo apt install mysql-server mysql-client
```

## サービススタートと確認
```
sudo systemctl start mysql.service
systemctl status mysql.service
```

```
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2025-03-03 10:46:20 JST; 30s ago
    Process: 4949 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 4957 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 4639)
     Memory: 364.4M
        CPU: 762ms
     CGroup: /system.slice/mysql.service
             └─4957 /usr/sbin/mysqld

 3月 03 10:46:19 systemd[1]: Starting MySQL Community Server...
 3月 03 10:46:20 systemd[1]: Started MySQL Community Server.
# ^C
```

## root パスワード設定
```
mysql -u root
```

```
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.41-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '****************';
Query OK, 0 rows affected (0.06 sec)

mysql> exit
```

## パスワードセキュリティの設定
```
sudo mysql_secure_installation
```

 ここでは、最初の２つの質問は、n にして、あとは全部、y にしました。
```

Securing the MySQL server deployment.

Enter password for user root: 

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: n
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : y
Success.


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? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

```

## パスワード付きで MySQL monitor へログイン
```
mysql -u root -p
```

```
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.41-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> 

```

 以上でスイッチできました。