2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

mysql 8.0 複数インスタンス @ CentOS7 + systemctl

Posted at

systemctl で制御できる mysql 複数だえもん

  • MySQL Community Server 8.0 はインストールしたてでまっさらの状態
# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
# mysql --version
mysql  Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)
  • SELinux で許可されてるmysqlのポートを確認
# semanage port -l | grep mysqld
mysqld_port_t                  tcp      1186, 3306, 63132-63164
  • ポート3307とかでLISTENさせたい場合(公式
# semanage port -a -t mysqld_port_t -p tcp 3307
# semanage port -l | grep mysqld
mysqld_port_t                  tcp      3307, 1186, 3306, 63132-63164
  • 公式に従って、/etc/my.cnf に複数インスタンスの設定を書く
# cat /etc/my.cnf
[mysqld]
port=3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysqld@another]
port=3307
datadir=/home/my/mysqldata
socket=/home/my/mysql.sock
log-error=/home/my/mysql.log
pid-file=/home/my/mysqld.pid
  • 無印mysqlデーモンを再起動、初期パスワードを確認、mysqlログイン、パスワード変更
# systemctl restart mysqld
# grep "temporary password" /var/log/mysqld.log
2021-04-09T02:54:27 [Server] A temporary password is generated for root@localhost: 1234;abcABC
# mysql -p
Enter password:
mysql> SET password for root@localhost='NewPassword';
mysql> FLUSH PRIVILEGES;
  • 第二のmysqlデーモンを起動、初期パスワードを確認
# systemctl start mysqld@another
# grep "temporary password" /home/my/mysqld.log
- 省略 -
  • ここで、ポート番号を利用してログインする時は、ホスト名として -h 127.0.0.1 を指定すること
  • mysql の localhost127.0.0.1 は振る舞いが違う(UNIX local socket, OR TCP/IP)
  • -h オプションを省略すると、-h localhost が採用される
# mysql -p -h 127.0.0.1 -P 3307
  • あえて UNIX socket でもよし
# mysql -p -S /home/my/mysql.sock
  • ログインしたらパスワードを変更しておく
  • デーモンの状態はこんな感じ
# ps -ef | grep mysqld
mysql  36689    1  0 00:03   00:00:47 /usr/sbin/mysqld
mysql  37545    1  0 00:11   00:00:44 /usr/sbin/mysqld --defaults-group-suffix=@another
  • 起動設定
# systemctl enable mysqld
# systemctl enable mysqld@another
  • 外から接続させるなら、Firewall にポートを登録
# cp /usr/lib/firewalld/services/mysql.xml /etc/firewalld/services/
# cat /etc/firewalld/services/mysql.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>MySQL</short>
  <description>MySQL Database Server</description>
  <port protocol="tcp" port="3306"/>
  <port protocol="tcp" port="3307"/>
</service>
# firewall-cmd --reload
2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?