Mysql配置數據庫主從複製

mysql實現主從複製的過程
使用的環境爲rhel7
主服務器ip 爲172.25.254.41
從服務器ip 爲172.25.254.42
首先配置好yum源,然後安裝好mariadb-server,設置好root密碼等等
進入/etc下配置好my.cnf文件
在master端配置如下

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
log-bin=mysql-bin   #開啓二進制日誌(必須)
server-id=41        #服務器ID號,必須是唯一

在slave端配置爲

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
log-bin=mysql-bin   #開啓二進制日誌(不是必須,但是爲了可以主從切換所以還是寫上去了)
server-id=42        #服務器ID號,必須是唯一

然後在兩臺主機上,重啓服務

systemctl restart mariadb

在master上的操作
進入mysql

grant replication slave on *.* to 'slave'@'172.25.254.42' identified by 'slave';

對slave登陸時創建一個登陸用戶並授權,最好不要用root用戶。
show master status;
然後查詢master的狀態

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |     1235 |              |                  |
+------------------+----------+--------------+------------------+

注意:在這一步之後不要對數據庫進行操作,不然File和Position都會出現變化,導致連接不上

在slave進行操作
進入mysql

 change master to master_host='172.25.254.41',master_user='slave',master_password='slave',master_log_file='mysql-bin.000002',master_log_pos=1235; 

與master建立連接,並且將登陸用戶信息加入,最後兩項就是寫在master中顯示的File和Position
start slave;
show slave status \G; #查看從服務器的狀態

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event  #從服務器的I/O狀態
                  Master_Host: 172.25.254.41            #主服務器的ip
                  Master_User: slave                #登陸的用戶
                  Master_Port: 3306             #開啓的端口
                Connect_Retry: 60               #重試等待時間
              Master_Log_File: mysql-bin.000002         #日誌文件
          Read_Master_Log_Pos: 1235             #位置
               Relay_Log_File: mariadb-relay-bin.000002  
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes              #必須爲yes
            Slave_SQL_Running: Yes              #必須爲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: 1235
              Relay_Log_Space: 825
              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       #主服務的server id

測試
在master上

create database xiaobai;
use xiaobai;
create table status( name varchar(5),gender char(2) );

在slave上

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| xiaobai            |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use xiaobai;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [xiaobai]> show tables;
+-------------------+
| Tables_in_xiaobai |
+-------------------+
| status            |
+-------------------+
1 row in set (0.00 sec)

最後結果爲
這裏寫圖片描述

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章