mysql基礎之mysql主從架構


一、概念

  主從多用於網站架構,因為主從的同步機制是異步的,數據的同步有一定延遲,也就是說有可能會造成數據的丟失,但是性能比較好,因此網站大多數用的是主從架構的數據庫,讀寫分離必須基於主從架構來搭建

二、配置主節點

1、配置准備

(1)yum源配置

[mysql57-community]
name=MySQL 5.7 Community Server  
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=0

(2)服務器1:192.168.11.7(主)

    服務器2:192.168.11.8(從)

2、下載mysql,修改配置文件(5.7版本與mariadb沖突,需要先刪除mariadb)

yum -y install mysql-com*server
[root@ren7 ~]# vim /etc/my.cnf.d/server.cnf 
[
mysqld] server-id=1    <<主從節點必須保證不能一樣 log-bin=mysql-bin <<定義二進制日志文件保存文件和格式
binlog_format=row <<主從節點保持一致,要么都是row,要么都是mixed

3、重啟mysql服務

[root@ren7 ~]# systemctl restart mysql

4、主節點上授權具有復制權限的用戶(創建主從連接賬號與授權)

mysql [(none)]> create user 'slave'@'%' identified by 'Slave123/';
Query OK, 0 rows affected (0.00 sec)

mysql [(none)]> grant replication slave on *.* to 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

5、查看當前二進制日志文件

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

三、配置從節點(修改從節點和主節點時間同步,可以使用NTP服務或者date直接設置)

1、修改配置文件

[root@ren8 ~]# vim /etc/my.cnf.d/server.cnf
###########################
[mysqld]
server-id=2
binlog_format=row
log-bin=mysql-bin ##########################

2、重啟mysql服務

[root@ren8 ~]# service mysql restart
Redirecting to /bin/systemctl restart mysql

3、登錄數據庫

[root@ren8 ~]# mysql_secure_installation
[root@ren8 ~]# mysql -u root -p123
Welcome to the mysql monitor.  Commands end with ; or \g.
Your mysql connection id is 24
Server version: 10.2.26-MariaDB mysql Server

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

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

4、建立主從連接

格式:CHANGE MASTER TO 選項
選項:
MASTER_HOST = 'host_name'         指定主服務的ip或者主機名
MASTER_USER = 'user_name'         指定主服務器的用戶名
MASTER_PASSWORD = 'password'      指定用戶名的密碼
MASTER_PORT = port_num            指定連接的端口,默認是3306
MASTER_CONNECT_RETRY = interval   指定連接失敗的時候的重試間隔時間
mysql [(none)]> change master to master_host='192.168.11.7', master_user='slave', master_password='Slave123/', master_log_file='mysql-bin.000001', master_log_pos=781;
Query OK, 0 rows affected (0.03 sec)

5、啟動從節點

mysql [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

6、查看從節點連接狀態

主要看IO以及SQL線程是否啟動

mysql [(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 'slave@192.168.11.7: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

問題解決:

slave_IO_Running:Connecting一直處於連接狀態

錯誤原因:
1、網絡不通
2、賬戶密碼錯誤
3、防火牆
4、mysql配置文件問題
5、連接服務器時語法錯誤
6、主服務器mysql權限

此處是防火牆的原因,在主從服務器上添加防火牆端口即可;

[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

 mysql [(none)]> stop slave;
 Query OK, 0 rows affected (0.00 sec)

 mysql [(none)]> start slave;
 Query OK, 0 rows affected (0.00 sec)

mysql [(none)]> show slave status\G;
*************************** 1. row ***************************
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

7、測試

--在主節點創建一個新表
mysql [(none)]> create database zhong;
Query OK, 1 row affected (0.00 sec)
--在從節點查看是否創建成功
mysql [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| zhong              |
+--------------------+
4 rows in set (0.00 sec)
--如果出現不同步可以執行以下步驟
stop slave 
set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM