Administrator
发布于 2024-07-23 / 22 阅读
0
0

Mysql Multi Instance On Machine

在一台机器上,按照多个mysql实例

refer to : https://dev.mysql.com/doc/refman/8.0/en/using-systemd.html#systemd-multiple-mysql-instances

下载rpm文件

manifest 如下:

mysql-community-common: http://repo.mysql.com/yum/mysql-8.0-community/el/8/x86_64/mysql-community-common-8.0.36-1.el8.x86_64.rpm

mysql-community-client-plugins: http://repo.mysql.com/yum/mysql-8.0-community/el/8/x86_64/mysql-community-client-plugins-8.0.36-1.el8.x86_64.rpm

mysql-community-libs: http://repo.mysql.com/yum/mysql-8.0-community/el/8/x86_64/mysql-community-libs-8.0.36-1.el8.x86_64.rpm

mysql-community-client: http://repo.mysql.com/yum/mysql-8.0-community/el/8/x86_64/mysql-community-client-8.0.36-1.el8.x86_64.rpm

mysql-community-icu-data-files: http://repo.mysql.com/yum/mysql-8.0-community/el/8/x86_64/mysql-community-icu-data-files-8.0.36-1.el8.x86_64.rpm

mysql-community-server: http://repo.mysql.com/yum/mysql-8.0-community/el/8/x86_64/mysql-community-server-8.0.36-1.el8.x86_64.rpm

开始安装

接下来,我们将这些文件,按照顺序安装。

rpm -ivh mysql-community-common-8.0.36-1.el8.x86_64.rpm

rpm -ivh mysql-community-client-plugins-8.0.36-1.el8.x86_64.rpm

rpm -ivh mysql-community-libs-8.0.36-1.el8.x86_64.rpm

rpm -ivh mysql-community-client-8.0.36-1.el8.x86_64.rpm

rpm -ivh mysql-community-icu-data-files-8.0.36-1.el8.x86_64.rpm

rpm -ivh mysql-community-server-8.0.36-1.el8.x86_64.rpm

在安装community-server时,可能会报以下错误:

[root@vm132 Mysql]# rpm -ivh mysql-community-server-8.0.36-1.el8.x86_64.rpm
warning: mysql-community-server-8.0.36-1.el8.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID a8d3785c: NOKEY
error: Failed dependencies:
        net-tools is needed by mysql-community-server-8.0.36-1.el8.x86_64
		

我们这里,使用yum的方式,来安转这个依赖:

yum install -y net-tools

修改/etc/my.cnf

默认情况下,/etc/my.cnf配置如下:

[root@VM-0-13-rockylinux system]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

可以看到,分别配置了datadir socket log-error pid-file 4个配置项

接下来,需要对/etc/my.cnf 进行修改,修改后,内容如下:

vim /etc/my.cnf 
[mysqld@replica01]
datadir=/var/lib/mysql-replica01
socket=/var/lib/mysql-replica01/mysql.sock
port=3306
log-error=/var/log/mysqld-replica01.log
interactive_timeout=3600
wait_timeout=3600
max_connections=1000

[mysqld@replica02]
datadir=/var/lib/mysql-replica02
socket=/var/lib/mysql-replica02/mysql.sock
port=3307
log-error=/var/log/mysqld-replica02.log
interactive_timeout=3600
wait_timeout=3600
max_connections=1000

启动服务

The replica names shown here use @ as the delimiter because that is the only delimiter supported by systemd.

Instances then are managed by normal systemd commands, such as:

systemctl start mysqld@replica01
systemctl start mysqld@replica02

To enable instances to run at boot time, do this:

systemctl enable mysqld@replica01
systemctl enable mysqld@replica02

Use of wildcards is also supported. For example, this command displays the status of all replica instances:

systemctl status 'mysqld@replica*'

启动Mysql 并进行密码初始化设置


# 查看replica01自动生成的密码
grep 'temporary password' /var/log/mysqld-replica01.log

mysql -uroot --port=3306 --socket=/var/lib/mysql-replica01/mysql.sock -p
 
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'WeiPeng@123';
 
 # 接下来,让远程客户端,也可以访问mysql server
use mysql;
select host from user where user='root';
update user set host = '%' where user ='root';
flush privileges;
quit



# 查看replica02自动生成的密码
grep 'temporary password' /var/log/mysqld-replica02.log

mysql -uroot --port=3307 --socket=/var/lib/mysql-replica02/mysql.sock -p
 
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'WeiPeng@456';
 
 # 接下来,让远程客户端,也可以访问mysql server
use mysql;
select host from user where user='root';
update user set host = '%' where user ='root';
flush privileges;
quit

开启firewall port & 测试访问

firewall-cmd --state
systemctl start firewalld.service 
firewall-cmd --add-port=3306/tcp --permanent
firewall-cmd --add-port=3307/tcp --permanent
firewall-cmd --reload


# 接下来,使用mysql-workbench,测试,是否能正常访问mysql-server

评论