您好,登錄后才能下訂單哦!
這篇文章主要介紹了Mysql如何實現(xiàn)主從配置和多主多從配置,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
我們現(xiàn)在模擬的是主從(1臺主機、一臺從機),其主從同步的原理,就是對bin-log二進制文件的同步,將這個文件的內容從主機同步到從機。
我們首先需要mysql主機(192.168.254.130)的/etc/my.cnf配置文件,添加如下配置:
#主機唯一ID server-id=1 #二進制日志 log-bin=mysql-bin #不需要同步的數(shù)據(jù)庫 binlog-ignore-db=mysql binlog-ignore-db=information_schema #同步的數(shù)據(jù)庫名稱 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)的配置文件。
從機的配置修改比較簡單:
#從機機器唯一ID server-id=2 #中繼日志 relay-log=mysql-relay
同樣修改配置后,我們重啟下從機
下面我們可以通過命令連接到mysql的命令端:
[root@localhost bin]# [root@localhost bin]# pwd /usr/local/mysql/bin [root@localhost bin]# ./mysql -uroot -p
1)、創(chuàng)建同步用戶
首先我們可以在主機創(chuàng)建一個專門用于主從同步用戶,通過命令:
GRANT REPLICATION SLAVE ON *.* TO 'SLAVE'@'%' IDENTIFIED BY '123456';
2)、查看同步文件狀態(tài)
然后我們通過show master status;查看主機的同步內容狀態(tài):
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)
1)、設置從機的主機
執(zhí)行如下命令,這里設置了我們與主機建立同步的相關信息
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 'slave@192.168.254.130: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這里有錯誤,我們就可以去看下日志報的什么問題了,目前我這個是因為同步用戶寫錯了才不能同步,按上面說的先停止同步重置,修改后同步命令,再操作一遍就可以了。
我們先在主機創(chuàng)建我們前面設置的要同步的數(shù)據(jù)庫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>
然后我們就能在從機看到這個庫了
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mycat | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
3、表數(shù)據(jù)的初始
下面我們進行表數(shù)據(jù)的測試
1)、主機
首先我們再主機建立表并插入數(shù)據(jù)
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)、修改配置
我們首先需要修改其原來的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]#
修改這個文件后我們需要重啟機器
這臺原來已經配置其連接30機器了,所以這次不用修改
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)、創(chuàng)建同步用戶
首先我們可以在主機創(chuàng)建一個專門用于主從同步用戶,通過命令:
GRANT REPLICATION SLAVE ON *.* TO 'SLAVE'@'%' IDENTIFIED BY '123456';
3)、查看狀態(tài)
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)、設置同步的狀態(tài)
下面我們運行其連接的主機(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主機執(zhí)行同步start slave;,也在編號3主機執(zhí)行同步start slave;。
1)、可能的問題(可略過)
現(xiàn)在我們測試,然后分別查看這兩臺的master狀態(tài)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機器修改配置,然后在查看其的狀態(tài),
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插入數(shù)據(jù)
下面我們再編號3插入數(shù)據(jù),看編號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處理數(shù)據(jù)
下面我們在編號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如何實現(xiàn)主從配置和多主多從配置”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。