MariaDB——(三) MariaDB 10.0.15 standard replication主從複製搭建

最近看了一下MariaDB的常規復制章節,就循序漸進的搭建了一下最簡單的主從複製。須要的硬件環境很簡單(在虛擬機VMware中搭建):mysql

    1:兩臺server:Master: 192.168.6.133  Slave:192.168.6.132sql

    2:網絡配置,這裏圖個簡單,直接關閉master的防火牆數據庫

[root@master Desktop]# service iptables stop


概覽實現主從複製須要完成的配置:服務器

    1:主從集羣裏面的每臺server須要有一個惟一的server_id,下面的配置中,將master的server_id設置爲1, slave1的server_id設置爲2;網絡

    2:master須要啓用二進制日誌,slave須要啓動relay日誌。ide

    3:master上建立一個用戶,供slave登陸到master上覆制二進制日誌數據。this

 

具體配置過程以下:spa

1:master配置。3d

    首先停掉mysql服務(用root用戶執行service mysql status查看是否在正在運行):日誌

[root@master Desktop]# service mysql stop
Shutting down MySQL.. SUCCESS!

    網上的教程都是在my.cnf中添加相關配置,這個文件初始內容以下:

[mariadb@master Desktop]$ cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

    若是在[client-server]節點添加配置項,會形成mysql沒法啓動,不知是什麼緣由,看了下錯誤日誌master.err文件也沒發現有用的信息。看這個文件的內容,server端和client端配置文件位於/etc/my.cnf.d/目錄下:

[mariadb@master Desktop]$ ls /etc/my.cnf.d
mysql-clients.cnf  server.cnf  tokudb.cnf

    修改server.cnf文件以下,完成服務端配置:

[mariadb@master Desktop]$ vi /etc/my.cnf.d/server.cnf
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
log-basename = master
log-bin = /var/lib/mysql/master.bin
binlog-format = row
server_id = 1
# This group is only read by MariaDB-10.0 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.0]

    (紅色部分爲新增內容)
    接下來啓動mysql服務:

[root@master mysql]# service mysql start
Starting MySQL. SUCCESS!

    登陸到mariadb:

[mariadb@master Desktop]$ mysql -uroot -proot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.0.16-MariaDB-log MariaDB Server

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

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

    受權用戶(這個用戶稍後會在配置slave節點用到)

MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'192.168.6.133' identified by 'replpass';
Query OK, 0 rows affected (0.00 sec)

2.slave配置

    首先停掉salve節點的mysql服務,方法同上。

    修改server.cnf文件,具體內容以下:

[mariadb@slave1 Desktop]$ vi /etc/my.cnf.d/server.cnf

   

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
server_id = 2
relay-log = /var/lib/mysql/relay-bin # This group is only read by MariaDB-10.0 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.0]

(紅色部分爲新增)
    啓動mysql服務

[root@slave1 Desktop]# service mysql status
 SUCCESS! MySQL running (1495)

    登陸到mysql數據庫,查看relay log中繼日誌狀態:

[root@slave1 Desktop]# mysql -uroot -proot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.0.16-MariaDB MariaDB Server

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

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

MariaDB [(none)]> show global variables like '%relay%';
+-----------------------+--------------------------+
| Variable_name         | Value                    |
+-----------------------+--------------------------+
| max_relay_log_size    | 1073741824               |
| relay_log             | /var/lib/mysql/relay-bin |
| relay_log_index       |                          |
| relay_log_info_file   | relay-log.info           |
| relay_log_purge       | ON                       |
| relay_log_recovery    | OFF                      |
| relay_log_space_limit | 0                        |
| sync_relay_log        | 0                        |
| sync_relay_log_info   | 0                        |
+-----------------------+--------------------------+
9 rows in set (0.00 sec)

(relay_log參數值是咱們設定的路徑下的文件,說明中繼日誌設定OK)
    鏈接到主服務器master

MariaDB [(none)]> change master to master_host='192.168.6.133', master_user='repluser', master_password='replpass';
Query OK, 0 rows affected (0.09 sec)

    在masterserver上查看master進程數量

MariaDB [(none)]> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+----------+
| Id | User | Host      | db   | Command | Time | State | Info             | Progress |
+----+------+-----------+------+---------+------+-------+------------------+----------+
|  4 | root | localhost | NULL | Query   |    0 | init  | show processlist |    0.000 |
+----+------+-----------+------+---------+------+-------+------------------+----------+
1 row in set (0.00 sec)

    在master上查看操做日誌狀態

MariaDB [(none)]> show master status;
+---------------+----------+--------------+------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| master.000002 |      526 |              |                  |
+---------------+----------+--------------+------------------+
1 row in set (0.00 sec)

    在slave上查看slave狀態

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.6.133
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: 
          Read_Master_Log_Pos: 4
               Relay_Log_File: relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: 
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 0
              Relay_Log_Space: 248
              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: 0
                Last_IO_Error: 
               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: 
1 row in set (0.00 sec)
View Code

    關鍵看Slave_IO_Running: No            Slave_SQL_Running: No 這兩個,如今是未啓動,接下來在salve上啓動slave節點:

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

    再次查看slave狀態

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.6.133
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master.000002
          Read_Master_Log_Pos: 709
               Relay_Log_File: relay-bin.000008
                Relay_Log_Pos: 993
        Relay_Master_Log_File: master.000002
             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: 709
              Relay_Log_Space: 1629
              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: 
1 row in set (0.00 sec)
View Code

            Slave_IO_State: Waiting for master to send event

            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

    從節點已經處於接收master節點發送事件的狀態,能夠進行主從複製了,如今去master上建立一個數據庫:

MariaDB [(none)]> create database testsync;
Query OK, 1 row affected (0.01 sec)

    去從節點查看,是否已經自動同步:

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testsync           |
+--------------------+
5 rows in set (0.02 sec)

    已經完成同步。再次查看主從狀態:

    其中read_master_log_pos也是838,表示主從狀態一致。

相關文章
相關標籤/搜索