mysql主从配置

接前文 https://www.cnblogs.com/wscsq789/p/12262969.html,这里配置主从库

主库配置:

[root@guangzhou my.cnf.d]# pwd
/etc/my.cnf.d

#新增同步配置
[root@guangzhou my.cnf.d]# vim server.cnf 
[mysqld]
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
log-bin=mysql-bin
binlog_format=mixed
server-id=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

#导出所有数据库数据,一会需要再从库导入
[root@guangzhou my.cnf.d]# mysqldump -uroot -p --all-databases > ~/sync_dump_all.sql 

#重启mariadb服务
[root@guangzhou my.cnf.d]# systemctl restart mariadb
[root@guangzhou my.cnf.d]# mysql -h 127.0.0.1 -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.2.31-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type help; or \h for help. Type \c to clear the current input statement.

#新增同步用户
MariaDB [(none)]> create user sync_user@% identified by 666;

#从库账号授权
MariaDB [(none)]> grant replication slave on *.* to sync_user@%;

#检查新建的账号
MariaDB [(none)]> select user,host from mysql.user;

#检查账号授权
MariaDB [(none)]> show grants for sync_user@%;

#打印主库状态
MariaDB [(none)]> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000001
        Position: 831
    Binlog_Do_DB: 
Binlog_Ignore_DB: mysql,information_schema
1 row in set (0.00 sec)

ERROR: No query specified

 

从库配置:

[root@VM_0_11_centos my.cnf.d]# pwd
/etc/my.cnf.d

#新增配置
[root@VM_0_11_centos my.cnf.d]# vim server.cnf
[mysqld]
server-id=3
read-only=true

#重启mariadb
[root@VM_0_11_centos my.cnf.d]# systemctl restart mariadb
[root@VM_0_11_centos my.cnf.d]# mysql -h 127.0.0.1 -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.2.31-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type help; or \h for help. Type \c to clear the current input statement.

#设置同步配置,此处master_log_file需要和主库配置项log-bin保持一致
MariaDB [(none)]> change master to master_host=‘172.16.0.15‘,master_user=‘sync_user‘, master_password=‘123456‘, master_log_file=‘mysql-bin.000001‘, master_log_pos=831;

#启动从库
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

#Slave_IO_Running和Slave_SQL_Running均为Yes说明设置成功
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.0.15
                  Master_User: sync_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 831
               Relay_Log_File: VM_0_11_centos-relay-bin.000005
                Relay_Log_Pos: 555
        Relay_Master_Log_File: mysql-bin.000001
             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: 831
              Relay_Log_Space: 1172
              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_SSL_Crl: 
           Master_SSL_Crlpath: 
                   Using_Gtid: No
                  Gtid_IO_Pos: 
      Replicate_Do_Domain_Ids: 
  Replicate_Ignore_Domain_Ids: 
                Parallel_Mode: conservative
                    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
1 row in set (0.00 sec)

ERROR: No query specified

 

 完成上面配置后,同时开两个窗口分别连接主从库服务器,做增删改前选择test库做下对比:

mysql主从配置

 

 主库新增测试表:

mysql主从配置

从库:

mysql主从配置

 

 

主库录入几条测试数据:

mysql主从配置

 

 再看下从库:

 

 

mysql主从配置

 

 目前看下来数据已经同步成功了,并且可以知道的是从库牵涉到连个线程,一个是io读取,一个是sql写入

 

mysql主从配置

上一篇:centos安装数据库


下一篇:linux c++ 使用mysql C API