1. 程式人生 > >MariaDB實現主從複製

MariaDB實現主從複製

MySQL之父Widenius先生離開了Sun之後,覺得依靠Sun/Oracle來發展MySQL,實在很不靠譜,於是決定另開分支,這個分支的名字叫做MariaDB。 MariaDB跟MySQL在絕大多數方面是相容的,對於開發者來說,幾乎感覺不到任何不同。目前MariaDB是發展最快的MySQL分支版本,新版本釋出速度已經超過了Oracle官方的MySQL版本。
1.環境準備:首先你要有兩臺相互ping的通的linux。大家可以使用VMware安裝一臺linux虛擬機器,然後使用克隆方法,克隆出另外一臺虛擬機器。這裡我使用的虛擬機器是centos 7。需要注意的一點是克隆出另外一臺虛擬機器後需要在新克隆出的虛擬機器的網路介面卡的高階選項中重新生成一個MAC地址。
2.安裝過程:有了兩臺虛擬機器以後,我們就可以開始安裝MariaDB了。首先我們要檢查系統中是不是有自帶的MariaDB,有的話解除安裝後再安裝。
解除安裝過程:
停止服務:systemctl stop mariadb
查詢安裝包:rpm -qa |grep mariadb
如果查詢到MariaDB的安裝資訊,就執行下面的命令,如果沒有,直接跳過。
rpm -e mariadb-server
rpm -e mariadb
rpm -e –nodeps mariadb-libs
解除安裝完成後,執行命令:
yum -y install mariadb mariadb-server
安裝完成後,拷貝/usr/share/mysql/my-huge.cnf到/etc目錄下。即:cp /usr/share/mysql/my-huge.cnf /etc/my.cnf,如果已經存在,選擇直接覆蓋即可。然後編輯該檔案:vim /etc/my.cnf,在[mysqld]後面新增一行:lower_case_table_names=1,設定MariaDB不區分表明大小寫。然後啟動MariaDB服務並設定開機自啟動。
systemctl start mariadb
systemctl enable mariadb
做完這些步驟後,執行指令碼:/usr/bin/mysql_secure_installation,按照提示操作資料庫即可。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the
password will be blank, so you should just press enter here. Enter current password for root (enter for none): 安裝後預設沒有root密碼,直接回車 OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] Y New password: 輸入root的新密碼 Re-enter new password: 新密碼確認 Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to
log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] 刪除匿名使用者 Y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] 關閉root遠端登入 Y ... Success! By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] 刪除test資料庫 Y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] 確定以上所有操作 Y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!

然後再另外一臺linux虛擬機器上重複以上操作,知道MariaDB安裝成功。
接下來我們就可以配置MariaDB的主從了。
3.設定MariaDB主從
3.1選擇一臺linux虛擬機器作為從節點,一臺linux虛擬機器作為主節點。然後到從節點上面做如下修改:vim /etc/my.cnf,修改server-id=2.
然後重啟從節點:systemctl restart mariadb
3.2到主節點上建立一個slave
登陸MariaDB資料庫:mysql -uroot -p密碼
執行以下格式的命令:
GRANT REPLICATION SLAVE ON .{所有許可權} TO ‘slave’@’%’{使用者名稱為slave,%為任意地址} identified by ‘slave’;
即:GRANT REPLICATION SLAVE ON . TO ‘slave’@’%’ IDENTIFIED BY ‘slave’;
在主節點查詢master的狀態:SHOW MASTER STATUS;
執行結果會像下面這樣:

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |     1294 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

記住這裡的File和Position資訊。
然後在從節點上登陸MariaDB資料庫,執行以下格式的命令:
CHANGE MASTER TO
MASTER_HOST=’主節點的IP地址’, MASTER_USER=’主節點授權的使用者’, MASTER_PASSWORD=’主節點授權的使用者的密碼’,MASTER_LOG_FILE=’剛才幾的File資訊,MASTER_LOG_POS=剛才的Position資訊;
即:

CHANGE MASTER TO MASTER_HOST='192.168.12.220',MASTER_USER='slave',MASTER_PASSWORD='slave',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=1294;

然後重啟主節點和從節點。
到從節點上執行命令:show slave status\G
不出意外的話將會看到下面的資訊:

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.12.220
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 245
               Relay_Log_File: localhost-relay-bin.000004
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000004
             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: 245
              Relay_Log_Space: 1111
              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)

注意其中的
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
這兩句。當你看到兩個Yes時,表明你的MariaDB資料庫的主從已經安裝成功了。
如果有些同學因為一些意外沒有成功,可以使用以下的方法解決:
首先停掉Slave服務:slave stop。然後到主伺服器上檢視主機的狀態:
記錄File和Position對應的值
進入master
mysql> show master status;

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |     1294 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

然後到slave伺服器上執行手動同步:

mysql> change master to 
> master_host='master_ip',
> master_user='user', 
> master_password='pwd', 
> master_port=3306, 
> master_log_file= mysql-bin.000003', 
> master_log_pos=1294;
1 row in set (0.00 sec)
mysql> start slave ;
1 row in set (0.00 sec)

然後即可正常使用。
接下來我們分別遠端連線兩臺MariaDB伺服器,並且在主節點上進行資料的增刪改,會發現從節點也跟著做了相應的操作。
如果你無法使用遠端連線連線到MariaDB伺服器,到MariaDB伺服器上執行如下命令允許遠端連線:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的密碼' WITH GRANT OPTION;  
FLUSH PRIVILEGES;

授權root使用者遠端登入。