First, the concept
Master-slave architecture used for the site, because the master-slave synchronization mechanism is asynchronous, synchronous data of a certain delays, that may cause loss of data, but the performance is better, so if your site is most used in a master-slave architecture database, separate read and write must be based on master-slave architecture to build
Second, the master node
1. Prerequisites
(1) yum source configuration
[mariadb] name = MariaDB baseurl = http://mirrors.ustc.edu.cn/mariadb/yum/10.2/centos7-amd64/ gpgkey=http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck=1
(2) Server 1: 192.168.11.7 (Main)
Server 2: 192.168.11.8 (from)
2, modify the configuration file
[ren7 the root @ ~] # Vim /etc/my.cnf.d/server.cnf
[ mysqld ] Server - ID = . 1 << from the master node can not be guaranteed the same log - bin = MySQL - bin
3, restart the mysql service
[root@ren7 ~]# systemctl restart mariadb
4, the authorized user has permission to copy the primary site (created from the primary connection and authorization account)
MariaDB [(none)]> create user 'slave01'@'%' identified by '123'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> grant replication slave on *.* to 'slave01'@'%'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
5, view the current binary log file
MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 781 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
Third, the configuration from the node
1, modify the configuration file
[root@ren8 ~]# vim /etc/my.cnf.d/server.cnf ########################### [mysqld] server-id=2 ##########################
2, restart the mysql service
[root@ren8 ~]# service mariadb restart Redirecting to /bin/systemctl restart mariadb.service
3, Log database
[root@ren8 ~]# mysql_secure_installation [root@ren8 ~]# mysql -u root -p123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 24 Server version: 10.2.26-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.
4, to establish the connection from the master
Format: the CHANGE MASTER the TO Options Options: MASTER_HOST = ' host_name ' designated primary service of ip or hostname MASTER_USER = ' user_name ' specifies the primary server's user name MASTER_PASSWORD = ' password ' Specifies the password for the username MASTER_PORT = port connection port_num specified, the default is 3306 MASTER_CONNECT_RETRY = interval the specified when a connection failure retry interval
MariaDB [(none)]> change master to master_host='192.168.11.7', master_user='slave01', master_password='123', master_log_file='mysql-bin.000001', master_log_pos=781; Query OK, 0 rows affected (0.03 sec)
5, starting from the node
MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec)
6, view the connection status from the node
Mainly to see the IO and SQL thread is started
MariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Connecting to master Master_Host: 192.168.11.7 Master_User: slave01 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 781 Relay_Log_File: ren8-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Connecting <<IO线程正在连接 Slave_SQL_Running: Yes <<SQL线程启动成功 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: 781 Relay_Log_Space: 256 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: 2003 Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60 maximum-retries: 86400 message: Can't connect to MySQL server on '192.168.11.7' (113 "No route to host") Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 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
problem solved:
slave_IO_Running: Connecting been in a connected state
Cause: 1 , No Network 2 , account password wrong 3 , firewall 4 , mysql profile issue 5 , syntax errors when connecting to the server 6 , the main server mysql privileges
Here is the reason the firewall, add a firewall port from the server to the master;
[root@ren7 ~]# firewall-cmd --add-port=3306/tcp success [root@ren7 ~]# firewall-cmd --add-port=3306/tcp --permanent success [root@ren8 ~]# firewall-cmd --add-port=3306/tcp success [root@ren8 ~]# firewall-cmd --add-port=3306/tcp --permanent success
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_Running: Yes Slave_SQL_Running: Yes
7, test
- Create a new table in the master node MariaDB [ (none) ] > the Create Database zhong; Query the OK, 1 Row affected ( 0.00 sec) - from node to see whether to create a successful MariaDB [ (none) ] > Show Databases; + - ------------------ + | Database | + - ------------------ + | information_schema | | MySQL | | performance_schema | | zhong | + - ------------------ + 4 rows in the SET ( 0.00 sec)
- If there is no synchronization can perform the following steps
stop slave set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; start slave