主頁 > 知識庫 > Mysql實現(xiàn)主從配置和多主多從配置

Mysql實現(xiàn)主從配置和多主多從配置

熱門標簽:html地圖標注并導(dǎo)航 400電話變更申請 北京金倫外呼系統(tǒng) 呂梁外呼系統(tǒng) 催天下外呼系統(tǒng) 武漢電銷機器人電話 大豐地圖標注app 南太平洋地圖標注 400電話辦理服務(wù)價格最實惠

我們現(xiàn)在模擬的是主從(1臺主機、一臺從機),其主從同步的原理,就是對bin-log二進制文件的同步,將這個文件的內(nèi)容從主機同步到從機。

一、配置文件的修改

1、主機配置文件修改配置

我們首先需要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]# 

修改主機的配置文件后,我們需要通過命令重啟下服務(wù):

[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)、創(chuàng)建同步用戶

首先我們可以在主機創(chuàng)建一個專門用于主從同步用戶,通過命令:

GRANT REPLICATION SLAVE ON *.* TO 'SLAVE'@'%' IDENTIFIED BY '123456';

2)、查看同步文件狀態(tài)

然后我們通過show master status;查看主機的同步內(nèi)容狀態(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)

2、從機操作

1)、設(shè)置從機的主機

執(zhí)行如下命令,這里設(shè)置了我們與主機建立同步的相關(guān)信息

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;

這里如果提示已經(jīng)設(shè)置了主機配置,可以通過stop slavereset 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這里有錯誤,我們就可以去看下日志報的什么問題了,目前我這個是因為同步用戶寫錯了才不能同步,按上面說的先停止同步重置,修改后同步命令,再操作一遍就可以了。

三、主從同步測試

1、主機創(chuàng)建庫

我們先在主機創(chuàng)建我們前面設(shè)置的要同步的數(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> 

2、從機查看庫

然后我們就能在從機看到這個庫了

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> 

可以看到我們的主從配置已經(jīng)成功了。

四、多主多從

我們還可以多主多從,例如我們一個主從序列是編號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)

這臺原來已經(jīng)配置其連接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。同時再重啟服務(wù)。

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)、設(shè)置同步的狀態(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)
我們需要設(shè)置其去同步編號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;。

4、測試查看

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> 

可以看到目前我們已經(jīng)同步成功了,在編號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>

可以看到其是相互同步的。

到此這篇關(guān)于Mysql實現(xiàn)主從配置和多主多從配置的文章就介紹到這了,更多相關(guān)Mysql 主從配置和多主多從配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • MySQL主從配置學(xué)習筆記
  • 基于Linux的mysql主從配置全過程記錄
  • MySQL5.7主從配置實例解析
  • Docker mysql 主從配置詳解及實例
  • 小記一次mysql主從配置解決方案
  • mysql數(shù)據(jù)庫互為主從配置方法分享

標簽:西寧 無錫 自貢 迪慶 龍巖 徐州 麗水 南充

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Mysql實現(xiàn)主從配置和多主多從配置》,本文關(guān)鍵詞  Mysql,實現(xiàn),主從,配置,和,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Mysql實現(xiàn)主從配置和多主多從配置》相關(guān)的同類信息!
  • 本頁收集關(guān)于Mysql實現(xiàn)主從配置和多主多從配置的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章