mysql- from the main building

Preparing the Environment

 

CPU name

IP

System / MySQL version

Character

node3

192.168.210.132

CentOS7.3/5.5.60

Master

node2

192.168.210.130

CentOS7.3/5.5.60

slave

 

A, master node

1, create a database to be synchronized

mysql> create database zn;
mysql> use zn;
mysql> create table zn(id int,name varchar(20));

2. Stop the database service

systemctl stop mariadb.service

3, edit my.cnf

[mysqld]
log-bin = bin- MySQL- Master # enable binary logging Server - ID = . 1 # native database ID Flag binlog - do -db = libraries zn # can be copied from the server, the database name need to synchronize the binary binlog -ignore-db = mysql # may not be copied from the server library

4, restart the database service

systemctl restart mariadb.service

5. Create a simultaneous users and authorized

MariaDB [(none)]> grant replication slave on *.* to slave@'%'  identified by "123";
MariaDB [(none)]> show master status; 
+-------------------------+----------+--------------+------------------+
| File                    | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------------+----------+--------------+------------------+
| mysql-bin-master.000001 |      384 | zn           | mysql            |
+-------------------------+----------+--------------+------------------+
MariaDB [(none)]> 
MariaDB [(none)]>
MariaDB [(none)]> show binlog events\G
*************************** 1. row ***************************
   Log_name: mysql-bin-master.000001
        Pos: 4
 Event_type: Format_desc
  Server_id: 1
End_log_pos: 245
       Info: Server ver: 5.5.60-MariaDB, Binlog ver: 4
*************************** 2. row ***************************
   Log_name: mysql-bin-master.000001
        Pos: 245
 Event_type: Query
  Server_id: 1
End_log_pos: 384
       Info: grant replication slave on *.* to slave@'%'  identified by "123"
2 rows in set (0.00 sec)

6, View binary file

[root@node3 my.cnf.d]# cd /var/lib/mysql/
[root@node3 mysql]# ls[root@node3 mysql]# ls
aria_log.00000001  ibdata1      ib_logfile1  mysql-bin-master.000001  mysql.sock          test
aria_log_control   ib_logfile0  mysql        mysql-bin-master.index   performance_schema  zn

7, will be synchronized database export, and sent to slave node, before copying to ensure consistent synchronization of database

mysqldump -uroot -p123 zn> zn.sql # can export the database 
scp zn.sql 192.168.210.130:/root

 

Two, slave node

1, two database servers mysql version to be consistent

MariaDB [(none)]> show variables like '%version%';
+-------------------------+----------------------+
| Variable_name           | Value                |
+-------------------------+----------------------+
| innodb_version          | 5.5.59-MariaDB-38.11 |
| protocol_version        | 10                   |
| slave_type_conversions  |                      |
| version                 | 5.5.60-MariaDB       |
| version_comment         | MariaDB Server       |
| version_compile_machine | x86_64               |
| version_compile_os      | Linux                |
+-------------------------+----------------------+
7 rows in set (0.00 sec)

MariaDB [(none)]> 

2, test the connection to the primary server is successful, only the rights to copy , can not see the other libraries

[root@node2 ~]# mysql -uslave -p123 -h 192.168.210.132
MariaDB [(none)]> 
MariaDB [(none)]> 
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> 

3, the normal into the database, and consistent with the primary database server

[root@node2 ~]# mysql -u root
MariaDB [(none)]> create database zn;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> exit
[root@node2 ~]# mysql -uroot zn<zn.sql

4, from the server is not necessary to open the bin-log log, modify the configuration file from the server:

[node2 the root @ ~ ] STOP # systemctl mariadb.service 
[node2 the root @ ~] # Vim / etc / the my.cnf 
[mysqld] 
Server - ID = 2    # from the server ID number, and not the same as the main ID, if a plurality of from the server, each must have a unique server-id value from the server, the server must not, and the other from the same master server. Server-id value may be considered similar to IP Address: The ID value uniquely identifying each replicated server instance of the server cluster.

5, the slave stop the service, set the master node ip, master_user synchronize user and password

[root@node2 ~]# mysql -u root 
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> change master to master_host='192.168.210.132',master_user='slave',master_password='123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> 
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.210.132
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin-master.000001
          Read_Master_Log_Pos: 384
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 675
        Relay_Master_Log_File: mysql-bin-master.000001
             Slave_IO_Running: Yes #负责与主机的io通信
            Slave_SQL_Running: Yes #负责自己的slave mysql进程
              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: 384
              Relay_Log_Space: 971
              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
1 row in set (0.00 sec)

MariaDB [(none)]>

 

Three, maste node

And then check the status of the primary server:

[root@node3 mysql]# mysql -u root
MariaDB [(none)]> show processlist\G *************************** 1. row *************************** Id: 11 User: slave Host: node2:37878 db: NULL Command: Binlog Dump Time: 343 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL Progress: 0.000

 

Fourth, if you encounter the main unsynchronized repair

(1) in the master node, the master from the look position of the bin-log, and then synchronize

MariaDB [(none)]> show master status; 
+-------------------------+----------+--------------+------------------+
| File                    | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------------+----------+--------------+------------------+
| mysql-bin-master.000001 |      384 | zn           | mysql            |
+-------------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> show binlog events\G
*************************** 1. row ***************************
   Log_name: mysql-bin-master.000001
        Pos: 4
 Event_type: Format_desc
  Server_id: 1
End_log_pos: 245
       Info: Server ver: 5.5.60-MariaDB, Binlog ver: 4
*************************** 2. row ***************************
   Log_name: mysql-bin-master.000001
        Pos: 245
 Event_type: Query
  Server_id: 1
End_log_pos: 384
       Info: grant replication slave on *.* to slave@'%'  identified by "123"
2 rows in set (0.00 sec)

MariaDB [(none)]>

(2) slave node performs under the MySQL command:

MariaDB [(none)]> STOP Slave; 
Query the OK, 0 rows affected ( 0.01 sec) 

MariaDB [(none)] >  
MariaDB [(none)] > Change Master to MASTER_LOG_FILE = ' MySQL-bin-master.000002 ' , MASTER_LOG_POS = 245 ; 
Query the OK, 0 rows affected ( 0.01 sec) 
# the results show master status above the primary server, recording the binary database server return, to synchronize the effect 
MariaDB [(none)] > MariaDB [(none) ]> Slave Start; 
Query the OK, 0 rows affected ( 0.00 sec) 

MariaDB [(none)] > Show Slave Status \ G
             Slave_IO_Running: Yes # if we are all yes, that means already in sync 
            Slave_SQL_Running: Yes

 

Guess you like

Origin www.cnblogs.com/cloudhere/p/10991520.html