MySQL主从复制同步配置操作

准备工作

准备两台服务器

主:1.1.1.1

从:1.1.1.2

我的都是5.7的版本,端口都是3306

创建同步用户

mysql> grant replication slave on *.* to 'replicate'@'1.1.1.2' identified by '123456';

mysql> flush privileges;

验证从是否能连上主

在从服务器(Slave)上输入如下指令(不要进到mysql里):

mysql -h1.1.1.1 -u replicate -p123456

修改mysql配置文件

sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
server-id = 1        //唯一id

log-bin=mysql-bin         //其中这两行是本来就有的,可以不用动,添加下面两行即可.指定日志文件

binlog-do-db = MyBlog   //记录日志的数据库(要备份的库)

binlog-ignore-db = mysql  //不记录日志的数据库(不要改)

重启mysql服务

service mysql restart

查看主服务器状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000010 |    20510 | MyBlog       | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

记下这个File名称和Postion,我这里已经刷新过了,所以不是000001

 

修改配置文件

server-id = 2

log-bin=mysql-bin

replicate-do-db = MyBlog

replicate-ignore-db = mysql,information_schema,performance_schema

重启mysql服务

service mysql restart

用change mster 语句指定同步位置

mysql>stop slave;          //先停步slave服务线程,这个是很重要的,如果不这样做会造成以下操作不成功。

mysql>change master to

>master_host='1.1.1.1',master_user='replicate',master_password='123456',

> master_log_file=' mysql-bin.000010',master_log_pos=20510;

注:master_log_file, master_log_pos由主服务器(Master)查出的状态

启动slave

mysql>start slave;

查看从服务器(Slave)状态

mysql> show slave status\G;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 101.132.70.184
                  Master_User: replicate
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000010
          Read_Master_Log_Pos: 154
               Relay_Log_File: iZuf60lr917wao15wbk40uZ-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000010
             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: 154
              Relay_Log_Space: 545
              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: 727bd933-f4a3-11e8-86e5-00163e042a24
             Master_Info_File: /var/lib/mysql/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: 
1 row in set (0.00 sec)

出现这个表示主备复制OK了

你可能感兴趣的:(Mysql)