mysql 主从复制

主master 

vim /etcmy.cnf

log-bin=mysql-bin
log-slave-updates=ture #开启从日志
server-id = 199

/etc/init.d/mysqld restart

创建复制用户并授权

grant replication slave on *.* to 'myslave'@'%' identified by 'chan123';

flush privileges;

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000009 | 2793 | | | |
+------------------+----------+--------------+------------------+-------------------+

从 slave

vim /etc/my.cnf

relay_log=relay-log-bin
relay_log_index=slave-relay-bin.index
server_id=1105

/etc/init.d/mysqld restart

 change master to master_host='192.168.1.99',master_user='myslave',master_password='chan123',master_log_file='mysql-bin.000009',master_log_pos=2793;

start slave;

show slave status G;

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

出现两个yes说明正常,可以测试了,

如果有一个不为NO,或为connecting,可以stop slave, reset slave 然后从新change master to

看日志进行排查,

[ERROR] Slave I/O: error connecting to master 'myslave@192.168.1.99:3306' - retry-time: 60  retries: 3, Error_code: 1045

用户名密码的问题,解决方法,先登陆一下试试,看是否可以登陆,mysql -umyslave -pchan123;看是否可以登陆,或查看权限show grants for 'myslave'@'%';

如果不能登陆也不能查看权限的话,看看是否存在用户名为空的用户,删掉它,重新授权就可以了;delete from mysql.user where user='';

perror 1045
MySQL error code 1045 (ER_ACCESS_DENIED_ERROR): Access denied for user '%-.48s'@'%-.64s' (using password: %s)

如果想保证与以前的数据一致,先在主服务器上进行锁表,不允许写入 flush tables with read lock;

在进行查看master status;记住position,解锁表 unlock tables;

从服务器上先进行恢复主上的数据,再进行change master to  ,

原文地址:https://www.cnblogs.com/haoge92/p/9323810.html