1. 程式人生 > >mysql的主從級聯的實現

mysql的主從級聯的實現

介紹:

主從級聯是為了減輕主資料庫的壓力,這樣更有利於實現負載均衡。如果我們沒有級聯伺服器,雖然也能實現主從同步的效果,但是我們不建議用,一方面是主資料庫的壓力過大,另一方面是效率過低。當我們有級聯時,我們的主伺服器,只需要將自己的資料同步給級聯即可,其餘的從伺服器由級聯伺服器去同步,這樣就能提高效率和減輕主資料庫的壓力了。

ip配置如下:

master(主)伺服器:172.18.254.223 cascade(級聯)伺服器:172.18.254.227 slave1(從)伺服器:172.18.254.228 實現思路: 先將cascade當做slave來對待,然後讓master與之同步,切記:cascade是有使用者的(master主從同步建立的目錄),然後將cascade當做主(master)來對slave1作主從同步。 master的配置檔案:

vim  /etc/my.cnf
server_id=1                #區分id沒什麼意義,但是必須有,且都不同
datadir=/mysql/data      #指定資料檔案的存放位置
log_bin=/mysql/logbin/log      #指定二進位制檔案的存放位置
innodb_file_per_table           #將二進位制檔案分開存放

cascade配置檔案:

vim  /etc/my.cnf
server_id=2            #區分id沒什麼意義,但是必須有,且都不同
innodb_file_per_table
datadir=/mysql/data
log_bin=/mysql/logbin/log       #和下面一起的
log_slave_updates                 #這個必須寫上,因為cascade是有二進位制檔案的

slave1配置檔案:

vim  /etc/my.cnf
server_id=3                    #區分id沒什麼意義,但是必須有,且都不同
datadir=/mysql/data
log_bin=/mysql/logbin/log
innodb_file_per_table

master的資料庫顯示:

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

MariaDB [(none)]> grant replication slave on *.* to 
[email protected]
'172.18.254.%' identified by 'centos'; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> select user from mysql.user; +-------+ | user | +-------+ | root | | zhang | | root | | | | root | | | | root | +-------+ 7 rows in set (0.00 sec)

cascade資料庫的顯示:

MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.254.223',
    ->   MASTER_USER='zhang',
    ->   MASTER_PASSWORD='centos',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE=‘log.000004',
    ->   MASTER_LOG_POS=245,
    ->   MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.07 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show master status;
+------------+----------+--------------+------------------+
| File       | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------+----------+--------------+------------------+
| log.000003 |      478 |              |                  |
+------------+----------+--------------+------------------+
1 row in set (0.00 sec)

slave1資料庫顯示:

MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.254.227',
    ->   MASTER_USER='zhang',
    ->   MASTER_PASSWORD='centos',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE=‘log.000003',
    ->   MASTER_LOG_POS=478,
    ->   MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.07 sec)
MariaDB [(none)]> start slave;

補充:刪除從的設定:

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

MariaDB [(none)]> reset slave all;
Query OK, 0 rows affected (0.17 sec)