Mysql實現主從配置和多主多從配置
我們現在模擬的是主從(1臺主機、一臺從機),其主從同步的原理,就是對bin-log二進制文件的同步,將這個文件的內容從主機同步到從機。
一、配置文件的修改
1、主機配置文件修改配置
我們首先需要mysql主機(192.168.254.130)的/etc/my.cnf配置文件,添加如下配置:
#主機唯一ID server-id=1 #二進制日志 log-bin=mysql-bin #不需要同步的數據庫 binlog-ignore-db=mysql binlog-ignore-db=information_schema #同步的數據庫名稱 binlog-do-db=mycat #二進制的格式 binlog_format=STATEMENT
我們看下目前整個my.cnf文件
[root@localhost Desktop]# cat /etc/my.cnf [mysqld] datadir=/usr/local/mysql/data basedir=/usr/local/mysql socket=/usr/local/mysql/data/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server-id=1 log-bin=mysql-bin binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-do-db=mycat binlog_format=STATEMENT [mysqld_safe] log-error=/usr/local/mysql/data/mysqld.log pid-file=/usr/local/mysql/data/mysqld/mysqld.pid [root@localhost Desktop]#
修改主機的配置文件後,我們需要通過命令重啟下服務:
[root@localhost support-files]# ls magic mysqld_multi.server mysql-log-rotate mysql.server [root@localhost support-files]# pwd /usr/local/mysql/support-files [root@localhost support-files]# ./mysql.server restart
然後我們修改下從機(192.168.254.131)的配置文件。
2、從機的配置
從機的配置修改比較簡單:
#從機機器唯一ID server-id=2 #中繼日志 relay-log=mysql-relay
同樣修改配置後,我們重啟下從機
二、mysql客戶端命令操作
下面我們可以通過命令連接到mysql的命令端:
[root@localhost bin]# [root@localhost bin]# pwd /usr/local/mysql/bin [root@localhost bin]# ./mysql -uroot -p
1、主機操作
1)、創建同步用戶
首先我們可以在主機創建一個專門用於主從同步用戶,通過命令:
GRANT REPLICATION SLAVE ON *.* TO 'SLAVE'@'%' IDENTIFIED BY '123456';
2)、查看同步文件狀態
然後我們通過show master status;查看主機的同步內容狀態:
mysql> show master status; +------------------+----------+--------------+--------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+--------------------------+-------------------+ | mysql-bin.000001 | 154 | mycat | mysql,information_schema | | +------------------+----------+--------------+--------------------------+-------------------+ 1 row in set (0.00 sec)
2、從機操作
1)、設置從機的主機
執行如下命令,這裡設置瞭我們與主機建立同步的相關信息
CHANGE MASTER TO MASTER_HOST='192.168.254.130', MASTER_USER='SLAVE', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=430;
這裡如果提示已經設置瞭主機配置,可以通過stop slave&reset master進行重置。
2)、啟動同步
下面我們再通過start slave開啟同步:
就可以看到:
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.254.130 Master_User: SLAVE Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 592 Relay_Log_File: mysql-relay.000002 Relay_Log_Pos: 482 Relay_Master_Log_File: mysql-bin.000001 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: 592 Relay_Log_Space: 685 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_UUID: 74397a99-accf-11eb-ae0d-000c2912d302 Master_Info_File: /usr/local/mysql/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec) mysql>
這裡我們可以看到Slave_IO_Running、Slave_SQL_Running都為YES,則成功瞭,如果是下面這種:
*************************** 1. row *************************** Slave_IO_State: Connecting to master Master_Host: 192.168.254.130 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 430 Relay_Log_File: mysql-relay.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Connecting 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: 430 Relay_Log_Space: 154 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: 1045 Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 1 Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 Master_UUID: Master_Info_File: /usr/local/mysql/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: 210505 00:18:08 Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
我們可以看到Last_IO_Error這裡有錯誤,我們就可以去看下日志報的什麼問題瞭,目前我這個是因為同步用戶寫錯瞭才不能同步,按上面說的先停止同步重置,修改後同步命令,再操作一遍就可以瞭。
三、主從同步測試
1、主機創建庫
我們先在主機創建我們前面設置的要同步的數據庫mycat:
mysql> create database mycat; Query OK, 1 row affected (0.00 sec) mysql> use mycat; Database changed mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mycat | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql>
2、從機查看庫
然後我們就能在從機看到這個庫瞭
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mycat | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
3、表數據的初始
下面我們進行表數據的測試
1)、主機
首先我們再主機建立表並插入數據
mysql> use mycat; Database changed mysql> mysql> create table `test1`( -> id int auto_increment not null primary key, -> name varchar(10) default null -> ); Query OK, 0 rows affected (0.03 sec) mysql> insert into test1(`id`,`name`) value(1,"petty"); Query OK, 1 row affected (0.16 sec) mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | +----+-------+ 1 row in set (0.00 sec) mysql>
2)、從機
下面我們在從機查看看有沒有成功同步:
mysql> use mycat; 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 mysql> show tables; +-----------------+ | Tables_in_mycat | +-----------------+ | test1 | +-----------------+ 1 row in set (0.00 sec) mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | +----+-------+ 1 row in set (0.00 sec) mysql>
可以看到我們的主從配置已經成功瞭。
四、多主多從
我們還可以多主多從,例如我們一個主從序列是編號1位主機、編號2為從機,然後編號3為主機、編號4為從機,同時編號1主機與編號3主機相互為主從,這樣就是其中主機一臺有問題,整個mysql集群還是能正常工作。
由於目前隻有3臺機,隻使用三臺來寫demo(一臺windows,兩臺linux)。
1、編號1主機(192.168.254.30)
1)、修改配置
我們首先需要修改其原來的etc/my.cnf文件,添加:
# 作為從機也修改其bin-log日志 log-slave-updates #自增長的幅度 auto-increment-increment=2 #自增長的開始位置 auto-increment-offset=1
整個文件的信息
[root@localhost Desktop]# cat /etc/my.cnf [mysqld] datadir=/usr/local/mysql/data basedir=/usr/local/mysql socket=/usr/local/mysql/data/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server-id=1 log-bin=mysql-bin binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-do-db=mycat binlog_format=STATEMENT log-slave-updates auto-increment-increment=2 auto-increment-offset=1 [mysqld_safe] log-error=/usr/local/mysql/data/mysqld.log pid-file=/usr/local/mysql/data/mysqld/mysqld.pid [root@localhost Desktop]#
修改這個文件後我們需要重啟機器
2、編號2從機(192.168.254.31)
這臺原來已經配置其連接30機器瞭,所以這次不用修改
3、編號3主機(192.168.254.1)
1)、修改配置文件
由於這臺機器是windows,所以我們需要修改其的my.ini文件,在其最後面添加
server-id=3 log-bin=mysql-bin binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-do-db=mycat binlog_format=STATEMENT log-slave-updates auto-increment-increment=2 auto-increment-offset=2
註意我們上面改瞭server-id,同時也改瞭其的增長開始點auto-increment-offset=2。同時再重啟服務。
2)、創建同步用戶
首先我們可以在主機創建一個專門用於主從同步用戶,通過命令:
GRANT REPLICATION SLAVE ON *.* TO 'SLAVE'@'%' IDENTIFIED BY '123456';
3)、查看狀態
mysql> show master status; +------------------+----------+--------------+--------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+--------------------------+-------------------+ | mysql-bin.000001 | 154 | mycat | mysql,information_schema | | +------------------+----------+--------------+--------------------------+-------------------+ 1 row in set (0.00 sec) mysql>
4)、設置同步的狀態
下面我們運行其連接的主機(30)信息
CHANGE MASTER TO MASTER_HOST='192.168.254.130', MASTER_USER='SLAVE', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=430;
5)、編號1主機同步(192.168.254.30)
我們需要設置其去同步編號3主機(192.168.254.1),即我們前面查看的編號3的(master status):
CHANGE MASTER TO MASTER_HOST='192.168.254.1', MASTER_USER='SLAVE', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;
然後我們在編號1主機執行同步start slave;,也在編號3主機執行同步start slave;。
4、測試查看
1)、可能的問題(可略過)
現在我們測試,然後分別查看這兩臺的master狀態show master status;。
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.254.1 Master_User: SLAVE Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 154 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes ........
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.254.130 Master_User: SLAVE Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 462 Relay_Log_File: LAPTOP-QR83QEC0-relay-bin.000003 Relay_Log_Pos: 675 Relay_Master_Log_File: mysql-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: .........
可以看到它們的同步都是yes。這裡可能有問題,我們需要自己解決,例如我在編號1機器修改配置,然後在查看其的狀態,
mysql> show master status; +------------------+----------+--------------+--------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+--------------------------+-------------------+ | mysql-bin.000002 | 462 | mycat | mysql,information_schema | | +------------------+----------+--------------+--------------------------+-------------------+ 1 row in set (0.00 sec) mysql>
我如果以這個消息去讓編號3機器同步編號1,就會報(因為我又運行瞭一條新的插入語句),但建表語句是在日志mysql-bin.000001,而這裡我因為重啟瞭,其有有新的mysql-bin.000002,所以有修改回瞭原來編號2的同步信息。
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.254.130 Master_User: SLAVE Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 462 Relay_Log_File: LAPTOP-QR83QEC0-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000002 Slave_IO_Running: Yes 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: 1146 Last_Error: Error 'Table 'mycat.test1' doesn't exist' on query. Default database: 'mycat'. Query: 'insert into test1(`id`,`name`) value(2,"TOm")'
2)、在編號3插入數據
下面我們再編號3插入數據,看編號1、2能不能看到
在編號3操作:
mysql> insert into test1(`id`,`name`) value(3,"kitt"); Query OK, 1 row affected (0.01 sec) mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | | 2 | TOm | | 3 | kitt | +----+-------+ 3 rows in set (0.00 sec) mysql>
在編號1查看
mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | | 2 | TOm | | 3 | kitt | +----+-------+ 3 rows in set (0.00 sec) mysql>
在編號2查看
mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | | 2 | TOm | | 3 | kitt | +----+-------+ 3 rows in set (0.00 sec) mysql>
可以看到目前我們已經同步成功瞭,在編號1中能查看到主機編號3的插入信息。
3)、編號1處理數據
下面我們在編號1操作查看
編號1:
mysql> insert into test1(`id`,`name`) value(4,"lisa"); Query OK, 1 row affected (0.00 sec) mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | | 2 | TOm | | 3 | kitt | | 4 | lisa | +----+-------+ 4 rows in set (0.00 sec) mysql>
編號3:
mysql> select * from test1; +----+-------+ | id | name | +----+-------+ | 1 | petty | | 2 | TOm | | 3 | kitt | | 4 | lisa | +----+-------+ 4 rows in set (0.00 sec) mysql>
可以看到其是相互同步的。
到此這篇關於Mysql實現主從配置和多主多從配置的文章就介紹到這瞭,更多相關Mysql 主從配置和多主多從配置內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!