LoginSignup
54
52

More than 3 years have passed since last update.

MySQL 8.0 初期設定

Last updated at Posted at 2020-04-14

環境

  • Ubuntu 20.04 LTS (Focal Fossa)
  • MySQL 8.0.19

Ubuntu バージョン

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu Focal Fossa (development branch)"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

MySQL インストール

$ sudo apt -y install mysql-server mysql-client
$ mysql --version
mysql  Ver 8.0.19-0ubuntu4 for Linux on x86_64 ((Ubuntu))

MySQL ステータス確認

$ systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-03-12 17:03:56 UTC; 1min 9s ago
   Main PID: 24346 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 1137)
     Memory: 371.9M
     CGroup: /system.slice/mysql.service
             └─24346 /usr/sbin/mysqld

補足: MySQL サービス

# MySQL サービスの起動
$ sudo systemctl start mysql
# MySQL サービスの停止
$ sudo systemctl stop mysql
# MySQL サービスの再起動
$ sudo systemctl restart mysql
# MySQL サービスの自動起動の有効化
$ sudo systemctl enable mysql
# MySQL サービスの自動起動の無効化
$ sudo systemctl disable mysql
# sudo vim /etc/my.cnf
/etc/my.cnf
[mysqld]
character_set_server = utf8mb4 # 文字コード
collation_server = utf8mb4_ja_0900_as_cs # 照合順序

# timezone
default-time-zone = SYSTEM
log_timestamps = SYSTEM

# Error Log
log-error = /var/log/mysql/mysql-error.log

# Slow Query Log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 5.0
log_queries_not_using_indexes = 0

# General Log
general_log = 1
general_log_file = /var/log/mysql/mysql-query.log

[mysql]
default-character-set = utf8mb4

[client]
default-character-set = utf8mb4

設定ファイルの変更を反映するにはMySQLを再起動する必要があります。

$ sudo systemctl restart mysql

MySQL 初期設定

初期パスワードは今回は secret と入力します。

$ 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: [空Enter]
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) : 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.

[リモートでrootログインを禁止しますか?]
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 rootユーザーでログイン

$ sudo mysql -u root -p
  • パスワードはさっき設定した secret
  • rootユーザーでログインするにはsudoが必要
  • exit でMySQLからログアウトできます

MySQL ユーザー一覧

> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | auth_socket           |
+------------------+-----------+-----------------------+

MySQL 文字コード設定

> SHOW VARIABLES LIKE '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

MySQL 照合順序設定

> SHOW VARIABLES LIKE '%collation%';
+-------------------------------+--------------------+
| Variable_name                 | Value              |
+-------------------------------+--------------------+
| collation_connection          | utf8mb4_0900_ai_ci |
| collation_database            | utf8mb4_0900_ai_ci |
| collation_server              | utf8mb4_0900_ai_ci |
| default_collation_for_utf8mb4 | utf8mb4_0900_ai_ci |
+-------------------------------+--------------------+
  • utf8mb4_0900_ai_ci MySQL8.0からの標準
    • 0900 Unicodeのバージョン 9.00
    • ai Accent Insensitiveの略称。アクセントの違いを無視する。
    • 「は」と「ぱ」は等しい文字として評価される。
    • ci Case Insensitiveの略称。大文字と小文字の違いを無視する。
    • 「あ」と「ぁ」は等しいと評価される。

MySQL データベース作成

> CREATE DATABASE shop;

shop という名前のデータベースを作成しています。

ユーザー作成

> CREATE USER phper@'%' IDENTIFIED BY 'secret';

phper@% というユーザーを作成しています。

% ホスト名をワイルドカードで指定しています。
これを設定すると外部からログインできます。
外部アクセスさせたくない場合は、 127.0.0.1localhost を設定します。

権限付与

> GRANT ALL PRIVILEGES on shop.* to phper@'%';
  • shop データベースのすべてのテーブルに対して、特定の権限レベルで使用可能なすべての権限を phper@% ユーザーに設定します。

確認

> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| phper            | %         | caching_sha2_password |
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | auth_socket           |
+------------------+-----------+-----------------------+

MySQL 8.0 リファレンスマニュアル

関連記事

54
52
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
54
52