linux mysql 主从配置 及数据同步

在完成主从配置前 先将主库设置只读

 flush tables with read lock;

 

最后执行 

UNLOCK TABLES

释放锁定

 

===================后加2021311=======================

 

 

 

1、主数据库配置/etc/my.conf

#主mysql服务器
server-id = 10

2、为了数据权限规范在主mysql上新建一个用来执行复制sql对的用户





两种新建方式
1、命令

先切入到mysql主服务器  mysql控制台

mysql -hlocalhost  -u用户名  -p密码 
 
重点  切入后mysql命令都是以;结尾的;别忘了敲

#新建用户命令
CREATE USER 'repl'@'从mysql服务器的地址最好是内网地址' IDENTIFIED BY '你给用户定义一个密码';
#分配权限
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'从mysql服务器的地址最好是内网地址';
#刷新权限命令
flush privileges;

 2、navicat来搞  简单  我发个图  不会的百度吧 

 主机就是从数据的地址  为啥的话就不说了 自己百度

2、从数据库配置/etc/my.conf

server-id = 20

#设置需要执行在主数据库服务器上哪些数据库的binglog日志
replicate-do-db = 你的数据库名称
replicate-do-db = 你的数据库名称
replicate-do-db = 你的数据库名称
replicate-do-db = 你的数据库名称
replicate-do-db = 你的数据库名称

 3、重启从mysql   

     

#linux服务器命令框执行
service  mysqld restart

4、切换到mysql主mysql控制台 查看下binglog的节点文件  

   

mysql> SHOW MASTER STATUS;

#看到如下显示
+------------------+-----------+--------------+------------------+
| File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000239 | 820761564 |              |                  |
+------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)

 

4、切换到mysql从 mysql控制台 第二步写了咋切入   完了执行下面命令   

CHANGE MASTER TO
        MASTER_HOST='从数据库地址',
        MASTER_USER='刚刚的用户名',
        MASTER_PASSWORD='刚刚的密码',
        MASTER_LOG_FILE='mysql-bin.000239',
        MASTER_LOG_POS=820761564;

5、在mysql从mysql控制台执行

#可以先看下配置从mysql的效果 
show slave status\G;


#如下
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 从数据库地址
                  Master_User: repl #设置的用户
                  Master_Port: 3306
                Connect_Retry: 5
              Master_Log_File: mysql-bin.000239
          Read_Master_Log_Pos: 822923899  #刚刚在主上看的节点
               Relay_Log_File: iZ2zeenvzv1yjmb7brr167Z-relay-bin.000001   #这个是从的复写                                                                            
                                                                           sql文件,虽然是    
                                                                           二进制的,你用工 
                                                                           具打开就是sql日 
                                                                           志
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000239 #刚刚看到的主文件
             Slave_IO_Running: No #因为没启动所以NO  表示拉取线程
            Slave_SQL_Running: No #因为没启动所以NO  表示执行复写线程
              Replicate_Do_DB: AA,BB,CC,DD,EE                              #从里面配置的需    
                                                                           要备份的数据库
          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: 822923899
              Relay_Log_Space: 107
              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: NULL
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: 0
1 row in set (0.00 sec)

ERROR: 
No query specified

6、启动拉取执行

#执行启动
slave start;

#结果如下

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 刚刚的地址
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 5
              Master_Log_File: mysql-bin.000239
          Read_Master_Log_Pos: 823211123
               Relay_Log_File: iZ2zeenvzv1yjmb7brr167Z-relay-bin.000002
                Relay_Log_Pos: 287477
        Relay_Master_Log_File: mysql-bin.000239
             Slave_IO_Running: Yes #这就对啦
            Slave_SQL_Running: Yes #这就对啦  
              Replicate_Do_DB: AA,BB,CC,DD,EE
          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: 823211123
              Relay_Log_Space: 287651
              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: 10
1 row in set (0.00 sec)

ERROR: 
No query specified

 注意  如果 Slave_SQL_Running: 是连接中  就等等  然后再查看   如果连接不上  就看看配置防火墙是否给了3306端口      还有一个 slave stop命令 如果到这都不知道这个命令的意思  我也就不多说啥了  

Guess you like

Origin blog.csdn.net/qq_34936628/article/details/111204680