UNIXCentOSDBサーバー MySQL

MySQL Community Server のインストールと設定

更新日:2017年9月30日

MySQL インストール

MySQLは非商用利用にて無償で利用できること、データベースサーバーの中でも高速に動作する性能や24時間365日ノンストップで運用できる安定性と、管理、バックアップ、リカバリーなどが簡単なことから世界で最も使用されているデータベースです。
CentOSではyumを使用して手軽にインストールできます。

MySQL互換データベースの削除

CentOS 7ではMySQL互換のmariaDBがあらかじめインストールされてています、また、yum標準リポジトリから直接MySQLをダウンロード出来なくなっています。MySQL公式リポジトリではダウンロード可能ですので、CentOS内のMySQL互換データベースとそのyumリポジトリを削除しましょう。

# yum remove mariadb-libs
# rm -rf /var/lib/mysql/
# yum remove mysql-server mysql-libs mysql-devel mysql*

MySQL 公式 リポジトリ追加

MySQL 公式リポジトリを追加しましょう。

# yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

MySQL Community Serverインストール

先ほど追加したMySQL 公式リポジトリからMySQL Community Serverをインストールしましょう。

# yum-config-manager --disable mysql56-community
# yum-config-manager --enable mysql57-community
# yum install mysql mysql-devel mysql-server
# yum install mysql mysql-utilities

MySQL バージョン確認

MySQL のバージョンを確認しましょう。

# mysqld --version
mysqld Ver 5.7.19 for Linux on x86_64 (MySQL Community Server (GPL))

MySQL 起動・自動実行

MySQL を起動しましょう。

# systemctl start mysqld.service

MySQL をサーバーが起動したときに自動的に起動できるにしましょう。

# systemctl enable mysqld.service

MySQL 初期設定

MySQL 5.7から安全面が考慮されて初期の起動でrootパスワード無しではログインできません、また、初期パスワードの状態ではデータベースに接続出来なくなりました。
rootパスワードの確認と、変更を行いましょう。

管理者(root)パスワードの確認

/var/log/mysqld.logに仮パスワードが記録されています。

# vi /var/log/mysqld.log
...
[Note] A temporary password is generated for root@localhost: 仮パスワード
...

管理者(root)パスワードの変更

rootパスワードの変更などの設定を行いましょう。
パスワードは、アルファベットと数字と記号を組み合わせる必要があります。また、リモートでのアクセスが禁止されますので、そのサーバーかログインしてからの操作になります。

# mysql_secure_installation
Securing the MySQL server deployment.
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: 新しいパスワード

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: validate_passwordプラグインに基づいた新しいパスワード

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

設定ファイル変更

設定ファイル(my.cnf)に必要な設定を行いましょう。
エンジンを InnoDB にしてトランザクション管理を可能に、文字コードを全てUTF8にしています。

# vi /etc/my.cnf
[mysqld]
...

default-storage-engine=InnoDB
innodb_file_per_table

character-set-server = utf8
collation-server = utf8_general_ci

[mysql]
default-character-set = utf8

[client]
default-character-set = utf8

MySQL 再起動

my.cnf で変更した設定を反映させるために MySQL を再起動して確認しましょう。

# systemctl restart mysqld.service
# mysql -u root -p
Enter password: root のパスワード
mysql> SHOW VARIABLES LIKE 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'innodb_file_per_table';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_file_per_table | ON |
+-----------------------+-------+
1 row in set (0.00 sec)