当然了,要想解决问题得先有环境。
一主一从的配置流程:
主:先配置DNS解析,实验环境可以利用 /etc/hosts 解析
vim /etc/hosts
192.168.122.45 master1
192.168.122.46 master2
从机不用在配置,通过scp发送到从机
scp /etc/hosts master2:/etc
1 启动二进制日志,服务器ID,GTID
vim /etc/my.cnf
log-bin
server-id=1
gtid_mode=ON
enforce_gtid_consistency=1
重启mysql
systemctl restart mysqld

  1. 授权复制用户rep
    grant replication slave,replication client on . to 'rep'@'192.168.122.%' identified by 'QianFeng@123';
    刷新
    flush privileges;
    3.备份数据(如果主机没有数据可不进行备份)
    mysqldump -p'QianFeng@123' --all-databases --single-transaction --master-data=2 --flush-logs > date +%F-mysql-all.sql
    scp 2018-3-20-mysql-all.sql master2:/tmp
    从:因DNS解析主机通过scp的方式发送过来,就不用在进行配置
    1.启动二进制日志,服务器ID,GTID
    vim /etc/my.cnf
    log-bin
    server-id=2
    gtid_mode=ON
    enforce_gtid_consistency=1
    重启mysql
    systemctl restart mysqld
  2. 还恢复手动同步数据
    set sql_log_bin=0;

         source /tmp/2018-3-20-mysql-full.sql
    
    3.设置主服务器
       mysql> change master to

    master_host='master1',br/>master_user='rep',
    master_password='QianFeng@123',
    master_auto_position=1;

        start slave;
    
        show slave status\G;
    
        到这里没什么问题一主一从就部署完成了,如果有错误可借鉴以下方法排错,希望能帮助到大家!
    
        例:
        *************************** 1. row ***************************  
           Slave_IO_State: Waiting for master to send event  
              Master_Host: 192.168.60.159  
              Master_User: backup  
              Master_Port: 3311  
            Connect_Retry: 60  
          Master_Log_File: mysql-bin.000012  
      Read_Master_Log_Pos: 274863854  
           Relay_Log_File: mysql-relay-bin.000007  
            Relay_Log_Pos: 2160037  
    Relay_Master_Log_File: mysql-bin.000012  
         Slave_IO_Running: Yes  
        Slave_SQL_Running: No  
          Replicate_Do_DB:   
      Replicate_Ignore_DB:   
       Replicate_Do_Table:   

    Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
    Replicate_Wild_Ignore_Table:
    Last_Errno: 1146
    Last_Error: Error executing row event: 'Table 'panda.t' doesn't exist'
    Skip_Counter: 0
    Exec_Master_Log_Pos: 2159824
    Relay_Log_Space: 274866725

        错误原因:主库删除的表在从库中不存在,导致从库在遇到删除不存在表的错误时无法继续同步。
        解决方法:利用slave-skip-errors参数,跳过对于的1146错误(这个参数是一个只读的,需要在配置文件中修改,并重启从库)
        1、在my.cnf的[mysqld]下面添加slave_skip_errors=1146

    2、重启从库 systemctl restart mysqld

    3、在从库上启动同步
    start slave;

    4、去掉my.cnf中的slave_skip_errors=1146

    5、重启从库 systemctl restart mysqld

    6、启动从库复制
    start slave;

    例:

    1. row
    Slave_IO_State: Waiting for master to send event
    Master_Host: 192.168.60.159
    Master_User: backup
    Master_Port: 3311
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000012
    Read_Master_Log_Pos: 274863854
    Relay_Log_File: mysql-relay-bin.000007
    Relay_Log_Pos: 2160037
    Relay_Master_Log_File: mysql-bin.000012
    Slave_IO_Running: No
    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: 1146
    Last_Error: Error executing row event: 'Table 'panda.t' doesn't exist'
    Skip_Counter: 0
    Exec_Master_Log_Pos: 2159824
    Relay_Log_Space: 274866725

    错误原因:主和从的UUID一致,需改动从机的UUID即可
    解决方法:
      1.在从机上 cd /var/lib/mysql
         ls 看到auto.cnf
             vim auto.cnf    随意改动一个数字即可
            保存并退出
            systemctl restart mysqld
        2.进入Mysql
            start slave;
    成功
    
    好了,到这里就结束了,有错的地方望大家批评和建议!!!