MySQL と競合しそうなMariaDBを削除しておく
$ sudo yum remove mariadb-libs
$ sudo rm -rf /var/lib/mysql
CentOS 7 に MySQL 公式の yum リポジトリを追加
$ sudo rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
MySQL のインストール
$ sudo yum install mysql-community-server mysql-community-client
サービスの起動
$ sudo systemctl start mysqld
初回起動時に自動設定されたパスワードを確認
$ sudo cat /var/log/mysqld.log | grep password
2018-10-17T02:30:35.176475Z 1 [Note] A temporary password is generated for root@localhost: password
mysql_secure_installationを実行
$ mysql_secure_installation
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
# => Yをプレスし、任意のパスワードに変更
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
# => Yをプレスし、匿名ユーザを削除
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
# => Yをプレスし、rootをローカルに限定
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
# => Yをプレスし、テストデータベースを削除
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
# => Yをプレスし、権限テーブルをリロード
設定ファイルの編集
$ sudo vi /etc/my.cnf
[mysqld] セクションに以下を追記
# ソケット通信だけでなく、LAN内の別マシンから接続させたい
port = 3306
bind-address = 0.0.0.0
# パスワードの有効期限切れを防ぐ
default_password_lifetime = 0
# パスワードのバリデーションを切る
validate_password_policy=LOW
validate_password_length=4
サービスを再起動
$ sudo systemctl restart mysqld
リモートアクセスを許可するユーザを作成
$ mysql -u root -p
mysql> CREATE USER 'username'@'192.168.%' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'username'@'192.168.%';
mysql> FLUSH PRIVILEGES;
CentOS7からファイアウォールが「iptables」から「firewalld」に変更されている。mysqlのポートを開放し、サービスを再起動。
$ sudo firewall-cmd --add-service=mysql --zone=public --permanent
$ sudo systemctl restart firewalld
別のマシンより接続テスト。繋がった。
$ mysql -h $MYSQL_SERVER_ADRESS -u username -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
参考にした記事
https://qiita.com/Thort/items/637b32b6c850a62af8de
https://qiita.com/haminiku/items/56fcb578d86abcd0b571