May 23, 2020

MySQLをCentOSにインストール

MySQL

リポジトリ追加

https://dev.mysql.com/downloads/repo/yum/

CentOSバージョンに合うものをインストール

CentOS7

$ sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

CentOS8

$ sudo dnf install  https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm

バージョン指定

リポジトリ追加直後はMySQL 8.0系をインストールするように向いている。

$ yum repolist | grep mysql
mysql-connectors-community/x86_64       MySQL Connectors Community           153
mysql-tools-community/x86_64            MySQL Tools Community                110
mysql80-community/x86_64                MySQL 8.0 Community Server           177

8.0系を無効化して5.7系を有効化する事もできる。

$ sudo yum-config-manager --disable mysql80-community
$ sudo yum-config-manager --enable mysql57-community

または直接.repoファイルを書き換える。

$ sudo vi /etc/yum.repos.d/mysql-community.repo

インストール

$ sudo yum install mysql-server

自動起動有効化

インストール直後は無効化されているので

$ systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

有効化する

$ sudo systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
$ systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead)

サービス起動

$ sudo systemctl start mysqld

仮のrootパスワード確認

$ sudo grep password /var/log/mysqld.log

CentOS8にMySQL8を入れると場所が違うらしい

$ sudo grep password /var/log/mysql/mysqld.log

初期設定

$ mysql_secure_installation

CentOS7 + MySQL8

$ mysql_secure_installation

Securing the MySQL server deployment.
# 上記で確認した仮のrootパスワード
Enter password for user root:

The existing password for the user account root has expired. Please set a new password.

New password:

Re-enter new password:
# ポリシーを満たしていないと怒られる、初期は8文字以上+数値、小文字、大文字、および特殊文字
 ... Failed! Error: Your password does not satisfy the current policy requirements

New password:

Re-enter new password:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.

Estimated strength of the password: 100
# パスワード強度に満足できるなら'n'で次に進む、もっと強度を上げたいなら'y'で再設定
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.
# anonymous usersは'y'で削除しておいた方がセキュア
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のリモートログインは'y'で禁止した方がセキュア
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.

# テスト用のDBは'y'で消しておいた方がセキュア
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.
# 'y'ですぐに再読み込みしておいた方が間違いがない
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

CentOS8 + MySQL8

$ 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: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
# 
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : n

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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!

© 2020 nissy-lab.com