MySQL主从

主机名 IP MySQL版本 centos版本
server 192.168.223.10 mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz centos7.2-1511
client 192.168.223.2

 一 主节点配置

复制代码
# .err结尾的文件为错误日志
[root@server ~]# ls -a /data/mysql/
.   auto.cnf  ib_logfile0  mysql               server.err  test
..  ibdata1   ib_logfile1  performance_schema  server.pid
# 修改配置文件 /etc/my.cnf  确定有log_bin 和server_id
[root@server ~]# vim /etc/my.cnf
log_bin=wsw
server_id = 41
[root@server ~]# service mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
# 可以看到多出log_bin名为开头的两个文件,wsw.000001、wsw.index
# 这两个文件为bin_log文件和索引文件 可以参考流程图
[root@server ~]# ls -a /data/mysql/
.         ibdata1      mysql               server.pid  wsw.index
..        ib_logfile0  performance_schema  test
auto.cnf  ib_logfile1  server.err          wsw.000001

# 创建主从用户 权限
mysql> grant replication slave on *.* to 'repl'@192.168.223.2 identified by '000000';
Query OK, 0 rows affected (0.00 sec)
# 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 查看server节点状态
mysql> show master status ;
+------------+----------+--------------+------------------+-------------------+
| File       | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------+----------+--------------+------------------+-------------------+
| wsw.000001 |      410 |              |                  |                   |
+------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
复制代码

二 从节点配置

复制代码
# 从节点修改配置文件,确保有server_id 这个字段,
# 字段id不要一样,一般以ip最后一位结尾
[root@client ~]# vim /etc/my.cnf
server_id = 42
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

# 创建连接, 主节点信息,bin_log文件名及大小 
mysql> change master to master_host='192.168.200.41',master_user='repl',master_password='000000',master_log_file='wsw.000001',master_log_pos=410;
Query OK, 0 rows affected, 2 warnings (0.04 sec)

mysql> start slave;
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: 192.168.223.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: wsw.000001
          Read_Master_Log_Pos: 410
               Relay_Log_File: client-relay-bin.000002
                Relay_Log_Pos: 277
        Relay_Master_Log_File: wsw.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:      # 同步那些库     这些配置都是可以写在著配置文件的my.cnf
          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: 410  
              Relay_Log_Space: 451
              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: 41
                  Master_UUID: 197dfb61-1310-11ec-af29-000c29b5c42c
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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
1 row in set (0.00 sec)

ERROR: 
No query specified
复制代码

三 测试

复制代码
# 主节点创建一个库,测试操作在这个库进行
mysql> create database wsw;
Query OK, 1 row affected (0.00 sec)

# 从节点查看同步信息
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wsw                |
+--------------------+
5 rows in set (0.00 sec)

# 000001文件大小为主节点操作命令组成
# 可以通过bin_log文件恢复数据
复制代码
posted @ 2021-12-06 15:59  蜡笔小新๑  阅读(25)  评论(0编辑  收藏  举报