Linux

執行多個 MySQL CentOS7 實例

  • November 12, 2019

我需要在我的 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

我在網上沖浪,發現了這個解決方案multiple instancesmysqld_multi。但我沒有找到解決問題的任何關鍵。我將如何執行我的實例?

我發現了為什麼我無法啟動我的兩個實例。有必要發表評論

$$ client $$文件中的部分並以適當的方式/etc/my.cnf寫入。這是我的做法: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

引用自:https://unix.stackexchange.com/questions/256285