mysql主从服务器设置

● 准备多个mysql(5.7.23)
● master设置

[root@localhost ~]# vi /etc/my.cnf
#在节点加入如下配置(然后重启mysql)
[mysqld]
#id
server-id=1
#二进制日志文件(注意是/  不是\)
log-bin="D:/MySQL/MySQL Server 5.5/data/mysql-bin"
#错误记录文件
log-error="D:/MySQL/MySQL Server 5.5/data/mysql-error"
#主从同步时 忽略的数据库
binlog-ignore-db=mysql
#(可选)指定主从同步时,同步哪些数据库
binlog-do-db=cok_db1

master授权哪些是从数据库:

mysql>GRANT REPLICATION slave,reload,super ON *.* TO 'root'@'192.168.0.%' IDENTIFIED BY 'root';
mysql>flush privileges ; 

查看master数据库的状态(每次在主从同步前,需要观察 主机状态的最新值)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 38848152 | cok_db1      |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

● slave设置(my.cnf加入如下)

[mysqld]
server-id=2
#mysql-bin此路径要注意是否有足够的权限
log-bin=mysql-bin
#replicate-do-db与master数据库binlog-do-db保持一致
replicate-do-db=cok_db1

从数据库进入mysql设置master

#master_log_pos : 此值就是master的Position值
#master_log_file : 对应master的File值
CHANGE MASTER TO 
MASTER_HOST = '192.168.0.118', 
MASTER_USER = 'root', 
MASTER_PASSWORD = 'root', 
MASTER_PORT = 3306,
master_log_file='mysql-bin.000005',
master_log_pos=38848152;

若执行上述语句报错

如果报错:This operation cannot be performed with a running slave; run STOP SLAVE first
解决:STOP SLAVE ;再次执行上条授权语句

● 开启主从同步

slave节点操作:
mysql> start slave;
#检验
mysql> show slave status \G
#然后会噼里啪啦列出一大堆东西,我们只需要关注如下:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#若两个都是YES,说明没问题
#否则:查看同列
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:

你可能感兴趣的:(mysql)