单节点搭建参考上一篇文章:
Linux下mysql8.0.16单节点搭建(一)
node1(master):192.168.200.57
node2(slave1):192.168.200.58
node3(slave2):192.168.200.59
1、master节点修改配置文件:
vi /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
server-id = 1
port = 3306
basedir = /mysqlsoft/mysql
datadir = /mysqlsoft/mysql/data
pid-file = /mysqlsoft/mysql/mysql.pid
socket = /tmp/mysql.sock
binlog_format = row
binlog_row_image=full
expire_logs_days=1 #修改binlog的保留时间
#skip-grant-tables #设置免密登录
max_connections=1000
max_connect_errors=1000
lower-case-table-names=1 #不区分大小
log_bin_trust_function_creators=1
sql_mode=only_full_group_by
wait_timeout = 1800 #阿里云值为7200
lock_wait_timeout = 3600 #阿里云值为31536000
#开启慢日志
slow_query_log = ON
slow_query_log_file =/mysqlsoft/mysql/slow-query.log
long_query_time = 1
binlog_expire_logs_seconds=2592000 #30天,binlog保存时间
log-bin=mysql-bin
[mysqld_safe]
log-error=/mysqlsoft/mysql/log/error.log
2、重启mysql:
mysql.server restart
3、进入mysql服务,创建复制账号:
su - mysql
mysql - u root -p(输入密码)
create user 'user_slave'@'%' identified with mysql_native_password by '123456';
4、授权user_slave的权限*.*表示所有库:
grant replication slave on *.* to 'user_slave'@'%';
grant replication client on *.* to 'user_slave'@'%';
5、刷新权限:
flush privileges;
6、查看master状态:
show master status;
1、修改配置文件:
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
server-id = 2 #(node3节点改成3,即server-id = 3)
port = 3306
basedir = /mysqlsoft/mysql
datadir = /mysqlsoft/mysql/data
pid-file = /mysqlsoft/mysql/mysql.pid
socket = /tmp/mysql.sock
binlog_format = row
binlog_row_image=full
expire_logs_days=1 #修改binlog的保留时间
#skip-grant-tables #设置免密登录
max_connections=1000
max_connect_errors=1000
lower-case-table-names=1 #不区分大小
log_bin_trust_function_creators=1
sql_mode=only_full_group_by
character_set_server = utf8
wait_timeout = 1800 #阿里云值为7200
lock_wait_timeout = 3600 #阿里云值为31536000
#开启慢日志
slow_query_log = ON
slow_query_log_file =/mysqlsoft/mysql/slow-query.log
long_query_time = 1
binlog_expire_logs_seconds=2592000 #30天,binlog保存时间
log-bin=mysql-bin
[mysqld_safe]
log-error=/mysqlsoft/mysql/log/error.log
2、登录数据库配置slave(master_host通过ip add命令获得;master_log_file和master_log_pos通过master节点步骤6获得):
su - mysql
mysql -u root -p 输入密码
change master to master_host='192.168.200.57',
master_user='user_slave',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1290;
3、进入mysql账号,创建复制用户:
su - mysql
mysql - u root -p(输入密码)
create user 'user_slave'@'%' identified with mysql_native_password by '123456';
4、授权user_slave的权限*.*表示所有库:
grant replication slave on *.* to 'user_slave'@'%';
grant replication client on *.* to 'user_slave'@'%';
5、刷新权限:
flush privileges;
6、启动从服务器:
start slave;
7、查看slave节点的状态(IO和SQL进程都是YES才代表成功);
SHOW slave STATUS \G;
1、在master创建用户,查看slave是否有新增的用户:
①、在master和slave上查询此时有的用户:
select host,user from mysql.user;
②、在master创建user_test用户,并查看slave节点是否新增用户:
CREATE USER 'user_test'@'%' IDENTIFIED BY 'test123';
GRANT ALL ON *.* TO 'user_test'@'%';
2、在master上新增库和表,观察slave节点是否同步:
①、在主库上新增库:
查看当前所有库:
show databses;
创建新的库:
create database test;
②、在主库上创建新的表并插入数据:
use test;
create table tm_1(aa int ,bb varchar(10),primary key(aa));
insert into tm_1(aa,bb)values(1,'A'),(2,'B');
select * from tm_1;
注意:
若出现下面错误,是因为master节点和slave节点的服务id是相同,则需要修改配置文件/etc/my.cnf,将各个节点的server-id的值改成不一样即可。
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).