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用戶遠程登錄。

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