LoginSignup
6
8

More than 5 years have passed since last update.

CentOS7 に MySQL5.7 のインストール~初期設定

Last updated at Posted at 2016-09-01

CentOS7 に MySQL5.7 のインストール~初期設定

mariadb 削除

root@localhost:~# rpm -e --nodeps mariadb-libs

MySQL 公式 yum リポジトリ追加

root@localhost:~# yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
root@localhost:~# yum info mysql57-community-release
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: www.ftp.ne.jp
 * epel: ftp.riken.jp
 * extras: www.ftp.ne.jp
 * remi-safe: mirrors.thzhost.com
 * updates: www.ftp.ne.jp
インストール済みパッケージ
名前                : mysql57-community-release
アーキテクチャー    : noarch
バージョン          : el7
リリース            : 8
容量                : 8.2 k
リポジトリー        : installed
提供元リポジトリー  : /mysql57-community-release-el7-8.noarch
要約                : MySQL repository configuration for yum
URL                 : http://dev.mysql.com
ライセンス          : GPLv2
説明                : Package for installation of setup/configuration files required for
                    : installation of MySQL packages by yum.
root@localhost:~# cp -ra /etc/yum.repos.d/mysql-community.repo /etc/yum.repos.d/mysql-community.repo.orig
root@localhost:~# vim /etc/yum.repos.d/mysql-community.repo
root@localhost:~# diff /etc/yum.repos.d/mysql-community.repo /etc/yum.repos.d/mysql-community.repo.orig
4,5c4
< #enabled=1
< enabled=0
---
> enabled=1
12,13c11
< #enabled=1
< enabled=0
---
> enabled=1
36,37c34
< #enabled=1
< enabled=0
---
> enabled=1
root@localhost:~# cat /etc/yum.repos.d/mysql-community.repo
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
#enabled=1
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
#enabled=1
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
#enabled=1
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-preview]
name=MySQL Tools Preview
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

MySQL 5.7 インストール

root@localhost:~# yum install --enablerepo=mysql57-community mysql-community-server mysql-community-devel
※mysql-community-devel は入れたい人だけ
root@localhost:~# mysql --version
    mysql  Ver 14.14 Distrib 5.7.14, for Linux (x86_64) using  EditLine wrapper

起動

root@localhost:~# systemctl start mysqld.service

自動起動設定

root@localhost:~# systemctl enable mysqld.service
root@localhost:~# systemctl list-unit-files -t service | grep mysqld
    mysqld.service                              enabled

初期設定

初期パスワードチェック

root@localhost:~# cat /var/log/mysqld.log | grep "A temporary password"
    2016-09-01T12:34:41.951776Z 1 [Note] A temporary password is generated for root@localhost: XXXXXXXXXXXX
※ XXXXXXXXXXXX のところにパスワードが表示されます。

validate_password のルールを緩める(ローカルサーバの場合とか)

root@localhost:~# mysql -u root -p
Enter password: (初期パスワード)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.14

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> SHOW VARIABLES LIKE 'validate_password%';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

(・д・)チッ
初期パスワードから変更しろと仰られるので

mysql> set password for root@localhost=password('root');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

(・д・)チッ
セキュリティが甘いと仰られるので、
とりあえず、

  • 最低1つは数字を
  • 最低1つは大文字英字を
  • 最低1つは子文字英字を
  • 最低1つは特殊文字を
  • 全部で8文字以上の文字列で

のパスワードをつける

mysql> set password for root@localhost=password('paSSwoRD|123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

( ´ー`)フゥ

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
6 rows in set (0.00 sec)

とりあえず文字数は4文字以内で、文字種などのルールを緩める
※ココらへんのルールは適宜決めてください。
※本番環境だったら、初期ルールに従っておいたほうがいい気がします。

mysql> SET GLOBAL validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)

mysql> SET GLOBAL validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password_dictionary_file    |       |
| validate_password_length             | 4     |
| validate_password_mixed_case_count   | 1     |
| validate_password_number_count       | 1     |
| validate_password_policy             | LOW   |
| validate_password_special_char_count | 1     |
+--------------------------------------+-------+
6 rows in set (0.00 sec)

mysql> exit;
Bye

my.cnf 設定

SET GLOBAL ~ の設定だと、mysqld の再起動後に設定が元に戻ってしまうので、
上記設定を、再起動後も使用したい場合は、
/etc/my.cnf に下記を追加

[mysqld]


validate_password_length=4
validate_password_policy=LOW

mysql_secure_installation でセキュリティ設定

root@localhost:~# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: (rootのパスワード)

初期パスワードを変更していない場合、パスワードを変更する

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

New password: (新しいパスワード)

Re-enter new password: (新しいパスワード)

rootパスワードを設定(変えないんなら変えなくてもいい)

The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: (新しいパスワード)

Re-enter new password: (新しいパスワード)

Estimated strength of the password: 50
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.

anonymous ユーザーを削除

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.

localhost以外の root ユーザを削除

※外部サーバからの接続を許可する場合は、rootユーザ以外を作りましょう

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

DB test の削除

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!

ユーザー設定

root@localhost しか使わなかったらしなくてもいいです。

mysql 接続

root@localhost:~# mysql -u root -p
Enter password: (rootパスワード)

ユーザ確認

mysql> SELECT HOST, USER FROM mysql.user;
+-----------+-----------+
| HOST      | USER      |
+-----------+-----------+
| localhost | mysql.sys |
| localhost | root      |
+-----------+-----------+
2 rows in set (0.00 sec)

権限確認

mysql> SHOW GRANTS for 'root'@'localhost';
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)

10.0.0.0/8 から接続できる hoge データベースに全権限を持った hoge ユーザを作ってみましょう

パスワードは test

mysql> CREATE DATABASE hoge DEFAULT CHARSET utf8mb4;
Query OK, 1 row affected (0.04 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| hoge               |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> CREATE USER 'hoge'@'10.0.0.0/255.0.0.0' IDENTIFIED BY 'test';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT HOST, USER FROM mysql.user;
+--------------------+-----------+
| HOST               | USER      |
+--------------------+-----------+
| 10.0.0.0/255.0.0.0 | ureru     |
| localhost          | mysql.sys |
| localhost          | root      |
+--------------------+-----------+
3 rows in set (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON `hoge`.* TO 'hoge'@'10.0.0.0/255.0.0.0';
Query OK, 0 rows affected (0.02 sec)

mysql> SHOW GRANTS FOR 'hoge'@'10.0.0.0/255.0.0.0';
+-----------------------------------------------------------------+
| Grants for hoge@10.0.0.0/255.0.0.0                              |
+-----------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'hoge'@'10.0.0.0/255.0.0.0'               |
| GRANT ALL PRIVILEGES ON `hoge`.* TO 'hoge'@'10.0.0.0/255.0.0.0' |
+-----------------------------------------------------------------+
2 rows in set (0.00 sec)

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

mysql> exit;
Bye

/etc/my.cnf 修正

root@localhost:~# cp -ra /etc/my.cnf /etc/my.cnf.orig
root@localhost:~# vim /etc/my.cnf
root@localhost:~# diff /etc/my.cnf /etc/my.cnf.orig
28,30d27
<
< character-set-server=utf8mb4
<

utf8mb4 … 4byte文字が使えるUTF8

MySQL 5.7.11 よりバージョンが低い場合

default_password_lifetime が360 に設定されています。
360日後に今のパスワードが使えなくなってしまうので、
(そんな面倒なものは)無効化しましょう。
※ MySQL 5.7.11 からは デフォルトで無効化されているようです。

root@localhost:~# vim /etc/my.cnf
root@localhost:~# diff /etc/my.cnf /etc/my.cnf.orig
28,31d27
<
< character-set-server=utf8mb4
< default_password_lifetime=0
<

上記以外の 設定はまだ調べきれてないので、これからちょこちょこいじってきます。
ひとまず、 character-set-serverdefault_password_lifetime だけ気をつけましょう。

再起動して設定を反映

root@localhost:~# systemctl restart mysqld.service

まだまだ、触りきれてはいないので、実際どれほどのもんかはわかってはいませんが、
とりあえず、インストール~初期設定までは以上です。

6
8
1

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
6
8