Ubuntu 20.04 に MySQL 8 をインストールする


はじめに

Ubuntu 20.04 に MySQL 8 をインストールするためのメモ。

環境

$ hostnamectl status
   Static hostname: ---
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ---
           Boot ID: ---
    Virtualization: microsoft
  Operating System: Ubuntu 20.04.2 LTS
            Kernel: Linux 5.4.0-113-generic
      Architecture: x86-64

セットアップ

1. インストール

$ sudo apt update \
  && sudo apt list --upgradable \
  && sudo apt -y autoremove \
  && sudo apt -y upgrade \
  && sudo apt -y install mysql-server libmysqlclient-dev

2. 初期設定

※ 2022/06/16 時点、本手順は不要のためスキップしてください。

rootユーザのパスワード以外は、全てデフォルト設定でいいのでEnter。

$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

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:
Please set the password for root here.

New password:

Re-enter new password:
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) :

 ... skipping.


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) :

 ... skipping.
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) :

 ... skipping.
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) :

 ... skipping.
All done!

3. MySQL にログイン

※ sudo をしないとログインできないので注意。

$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.29-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2022, 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>

4. ユーザを追加

username と userpassword は、環境に合わせて変更すること。

mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'userpassword';
Query OK, 0 rows affected (0.05 sec)

mysql> GRANT all ON *.* TO 'username'@'localhost';
Query OK, 0 rows affected (0.03 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)

mysql> exit
Bye

5. 追加ユーザでログイン

ユーザを追加の手順で入力したユーザ名とパスワードでログイン。

$ mysql -u username -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.29-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2022, 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>

修正履歴

  • 2022/06/16
    • mysql_secure_installationが不要になった旨を追記。
    • 加筆時点のMySQL最新バージョンに出力内容を更新。