运行MySQL CentOS7的多个实例

运行MySQL CentOS7的多个实例

我需要在我的 PC 上运行多个 MySQL 实例。我这样做了:

yum install mariadb-server mariadb=>安装mysql

mysql_install_db --datadir=/var/lib/mysql=> 使用 mysql_install_db 为第一个实例创建 datadir

mysql_install_db --datadir=/var/lib/mysql2=> 使用 mysql_install_db 为第二个实例创建 datadir

chown -R mysql:mysql /var/lib/mysqlchown 所有者用户:所有者组

chown -R mysql:mysql /var/lib/mysql2chown 所有者用户:所有者组

然后我修改了多个实例的 my.cnf 文件:

[client]
#password       = your_password
port            = 3306
socket          = /var/lib/mysql/mysql.sock

# Here follows entries for some specific programs

# The MariaDB server
[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin

[mysqld0]
port            = 3306
socket          = /var/lib/mysql/mysql.sock
pid-file        = /var/run/mysqld/mysqld.pid
datadir         = /var/lib/mysql
server-id = 1
log-bin = mysql-bin
binlog_format = mixed

[mysqld2]
port            = 3307
socket          = /var/lib/mysql2/mysql.sock
pid-file        = /var/run/mysqld/mysqld2.pid
datadir         = /var/lib/mysql2
server_id       = 2
skip-log-bin

然后我使用这个命令mysqld_multi report并得到了这个:

Reporting MySQL servers
MySQL server from group: mysqld0 is not running
MySQL server from group: mysqld2 is not running

然后我使用mysqld_multi startandmysqld_multi report再次收到相同的消息:

Reporting MySQL servers
MySQL server from group: mysqld0 is not running
MySQL server from group: mysqld2 is not running

我在网上冲浪并找到了这个解决方案多个实例mysqld_multi。但我没有找到解决问题的关键。我要做什么来运行我的实例?

答案1

我发现为什么我无法启动我的两个实例。有必要对/etc/my.cnf文件中的 [client] 部分进行注释并以正确的方式unit编写。/etc/systemd/system这是我的做法:

/etc/my.cnf 文件的配置:

[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin

[mysqld1]
user    = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port=3306
datadir=/var/lib/mysql
server_id=1
log-bin=mysql-bin
binlog_format=mixed

[mysqld2]
user = mysql
pid-file = /var/run/mysqld/mysqld2.pid
socket = /var/run/mysqld/mysqld2.sock
port=3307
datadir=/var/lib/mysql2
server_id=2
skip-log-bin

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

/etc/systemd/system/mysqld_multi_service.service的配置:

[Unit]
Description= mysqld_multi_service

[Service]
Type=simple
ExecStart=/usr/bin/mysqld_multi start
ExecStop=/usr/bin/mysqld_multi stop
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

然后systemctl enable mysqld_multi_service.servicesystemctl start mysqld_multi_service.service。后全部执行此步骤后,您可以使用以下命令连接到第一个实例:

mysql -u root -S /var/run/mysqld/mysqld.sock

或到第二个实例:

mysql -u root -S /var/run/mysqld/mysqld2.sock

答案2

来自MySQL 文档,看起来 mysql_multi 不应该再使用了:

由于 systemd 能够在安装了 systemd 支持的平台上管理多个 MySQL 实例,因此 mysqld_multi 和 mysqld_multi.server 是不必要的,因此不会安装。对于 RPM 平台的 MySQL 5.7.13 和 Debian 平台的 MySQL 5.7.19 来说都是如此。

相反,您应该使用 systemd 来管理您在配置文件中设置的实例my.cnf。请参阅Configuring Multiple MySQL Instances Using systemd提供的链接中的部分。

相关内容