MySQL8.0单机器多实例主从安装配置
MySQL8.0安装
下载MySQL rpm安装文件,链接https://dev.mysql.com/downloads/mysql/
安装mysql前先yum install libaio* -y,不然可能会缺少依赖。
解压开有多个rpm文件,仅需安装4个,按顺序安装,记得加–nodeps,不然会报缺少so依赖问题
rpm -ivh mysql-community-common-8.0.22-1.el6.x86_64.rpm --nodeps
rpm -ivh mysql-community-libs-8.0.22-1.el6.x86_64.rpm --nodeps
rpm -ivh mysql-community-client-8.0.22-1.el6.x86_64.rpm --nodeps
rpm -ivh mysql-community-server-8.0.22-1.el6.x86_64.rpm --nodeps
配置双实例
MySQL主要由/etc/my.cnf来控制,一个实例对应一个配置文件。
----配置master: 修改/etc/my.cnf,配置参考如下:
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin=mysql-bin
#必须指定id,master这儿指定1,slave为2,两个可以随意,但值必须不同
server-id=1
#指定要主备的数据库名称test
binlog-do-db = test
----配置slave
#首先为slave建立目录
mkdir -p /opt/mysql_data/3307/{data,tmp,binlog,innodb_ts,innodb_log}
#为新实例复制一份my.cnf
cp /etc/my.cnf /etc/my3307.cnf
#生成log文件供slave使用
echo "" > /opt/mysql_data/3307/mysqld-3307.log
#权限
chown -R mysql:mysql /opt/mysql_data
修改/etc/my3307.cnf,配置参考如下:
datadir=/opt/mysql_data/3307/data
tmpdir=/opt/mysql_data/3307/tmp
log-bin=/opt/mysql_data/3307/binlog/mysql-bin
innodb_data_home_dir=/opt/mysql_data/3307/innodb_ts
innodb_log_group_home_dir=/opt/mysql_data/3307/innodb_log
socket=/opt/mysql_data/3307/mysql-3307.sock
mysqlx_socket=/opt/mysql_data/3307/mysql-3307.sock
port = 3307
mysqlx_port = 33070
server_id = 2
log-error=/opt/mysql_data/3307/mysqld-3307.log
pid-file=/opt/mysql_data/3307/mysqld-3307.pid
—启动Master并配置
#启动数据库
systemctl start mysqld.service
#查看初始化密码,若登不上也可在/etc/my.cnf文件追加skip-grant-tables先免密登录
#这个日志文件很关键,数据库初始化、启动等log都在此,可通过log文件定位问题
grep "password" /var/log/mysqld.log
#登录数据库
mysql -u root -p
#配置
#修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
#创建备份用户(实际生产环境建议指定ip并控制用户权限)
CREATE USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
ALTER USER 'repl'@'%' PASSWORD EXPIRE NEVER;
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
#查看master状态
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 156 | test | | |
+------------------+----------+--------------+------------------+-------------------+
—初始化slave并启动
mysqld --defaults-file=/etc/my3307.cnf --initialize --user=mysql
/usr/bin/mysqld_safe --defaults-file=/etc/my3307.cnf --user=mysql &
#查看初始化密码
grep "password" /opt/mysql_data/3307/mysqld-3307.log
#登录数据库
mysql -u root -p -S /opt/mysql_data/3307/mysql-3307.sock
#执行同步SQL语句(MASTER_LOG_FILE='mysql-bin.000005',MASTER_LOG_POS=154由上述master status所得)
CHANGE MASTER TO MASTER_HOST='ip地址', MASTER_USER='repl',MASTER_PASSWORD='密码',MASTER_LOG_FILE='mysql-bin.000005',MASTER_LOG_POS=154;
#启动slave
start slave;
#查看slave状态,主要关注Slave_IO_Running,Slave_SQL_Running,last_Error,Last_SQL_Error
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: ip地址
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 2820
Relay_Log_File: host-10-32-46-70-relay-bin.000002
Relay_Log_Pos: 990
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 2820
Relay_Log_Space: 1210
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: d8df2388-3891-11eb-b9f6-fa163ebeebc8
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
记得slave先同步master表结构和数据,然后修改master数据,验证slave。如果没有成功,通过show slave status\G;
查看报错日志定位问题。