1. 程式人生 > >MySQL系列詳解七:MySQL實現雙主架構演示-技術流ken

MySQL系列詳解七:MySQL實現雙主架構演示-技術流ken

back span procedure eve mysql- ack community chang tee

前言

在企業中,數據庫高可用一直是企業的重中之重,中小企業很多都是使用mysql主從方案,一主多從,讀寫分離等,但是單主存在單點故障,從庫切換成主庫需要作改動。因此,如果是雙主或者多主,就會增加mysql入口,增加高可用。不過多主需要考慮自增長ID問題,這個需要特別設置配置文件,比如雙主,可以使用奇偶,總之,主之間設置自增長ID相互不沖突就能完美解決自增長ID沖突問題。

MySQL雙主(主主)架構方案思路是

1.兩臺mysql都可讀寫,互為主備,默認只使用一臺(masterA)負責數據的寫入,另一臺(masterB)備用;

2.masterA是masterB的主庫,masterB又是masterA的主庫,它們互為主從;

3.兩臺主庫之間做高可用,可以采用keepalived等方案(使用VIP對外提供服務);

4.所有提供服務的從服務器與masterB進行主從同步(雙主多從);

5.建議采用高可用策略的時候,masterA或masterB均不因宕機恢復後而搶占VIP(非搶占模式);

演示:MySQL雙主架構實現

環境:

主1服務器端IP: 10.220.5.137

主2服務器端IP: 10.220.5.138

1.首先需要修改主服務器端配置文件

 1 #my.cnf
  2 [client]
  3 port            = 3306
  4 socket          = /tmp/mysql3306.sock
  
5 6 [mysql] 7 #prompt="\\u@\\h:\\p [\\d]>" 8 #pager="less -i -n -S" 9 #tee=/opt/mysql/query.log 10 no-auto-rehash 11 12 [mysqld] 13 #misc 14 user = mysql 15 basedir = /usr/local/mysql 16 datadir = /data/mysql/mysql3306/data 17 port = 3306 18 socket = /tmp/mysql3306.sock 19 event_scheduler = 0
20 #skip-grant 21 22 tmpdir = /data/mysql/mysql3306/tmp 23 #timeout 24 interactive_timeout = 300 25 wait_timeout = 300 26 #auto_increment_offset = 2 27 #auto_increment_increment = 2 28 29 #character set 30 character-set-server = utf8 32 open_files_limit = 65535 33 max_connections = 100 34 max_connect_errors = 100000 35 lower_case_table_names =1 36 auto_increment_offset=1 <<從1開始增長 37 auto_increment_increment=2 <<每次增長為2,即為奇數 38 39 #symi replication 40 41 #rpl_semi_sync_master_enabled=1 42 #rpl_semi_sync_master_timeout=1000 # 1 second 43 #rpl_semi_sync_slave_enabled=1 44 45 #logs 46 log-output=file 47 slow_query_log = 1 48 slow_query_log_file = slow.log 49 log-error = error.log 50 log_warnings = 2 51 pid-file = mysql.pid 52 long_query_time = 1 53 #log-slow-admin-statements = 1 54 #log-queries-not-using-indexes = 1 55 log-slow-slave-statements = 1 56 57 #binlog 58 #binlog_format = STATEMENT 59 binlog_format = row 60 server-id = 1003306 61 log-bin = /data/mysql/mysql3306/logs/mysql-bin 62 binlog_cache_size = 4M 63 max_binlog_size = 256M 64 max_binlog_cache_size = 1M 65 sync_binlog = 0 66 expire_logs_days = 10 67 #procedure 68 log_bin_trust_function_creators=1 69 70 # 71 gtid-mode = on 72 enforce-gtid-consistency=1 73 74 75 #relay log 76 skip_slave_start = 1 77 max_relay_log_size = 128M 78 relay_log_purge = 1 79 relay_log_recovery = 1 80 relay-log=relay-bin 81 relay-log-index=relay-bin.index 82 log_slave_updates 83 #slave-skip-errors=1032,1053,1062 84 #skip-grant-tables 85 86 #buffers & cache 87 table_open_cache = 2048 88 table_definition_cache = 2048 89 tabl90 max_heap_table_size = 96M 91 sort_buffer_size = 128K 92 join_buffer_size = 128K 93 thread_cache_size = 200 94 query_cache_size = 0 95 query_cache_type = 0 96 query_cache_limit = 256K 97 query_cache_min_res_unit = 512 98 thread_stack = 192K 99 tmp_table_size = 96M 100 key_buffer_size = 8M 101 read_buffer_size = 2M 102 read_rnd_buffer_size = 16M 103 bulk_insert_buffer_size = 32M 104 105 #myisam 106 myisam_sort_buffer_size = 128M 107 myisam_max_sort_file_size = 10G 108 myisam_repair_threads = 1 109 110 #innodb 111 innodb_buffer_pool_size = 100M 112 innodb_buffer_pool_instances = 1 113 innodb_data_file_path = ibdata1:100M:autoextend 114 innodb_flush_log_at_trx_commit = 2 115 innodb_log_buffer_size = 8M 116 innodb_log_file_size = 100M 117 innodb_log_files_in_group = 3 118 innodb_max_dirty_pages_pct = 50 119 innodb_file_per_table = 1e_open_cache = 2048 120 innodb_rollback_on_timeout 121 innodb_status_file = 1 122 innodb_io_capacity = 100 123 transaction_isolation = READ-COMMITTED 124 innodb_flush_method = O_DIRECT

2.啟動mysql並創建復制用的用戶

[root@ken ~]# mysqld &              <<啟動mysql
[4] 2896 
[root@ken ~]# mysql -uroot -p       <<登錄mysql
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type help; or \h for help. Type \c to clear the current input statement.

mysql> grant replication slave on *.* to ken@% identified by xx;  <<創建復制用的用戶

3.檢查主2的二進制日誌文件位置,在主2服務器上面執行下面的命令獲取到當前二進制日誌記錄位置

MySQL [ken]> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000004            
         Position: 1558
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-5,
c01b1811-d7b3-11e8-8698-000c29492f7b:1-3
1 row in set (0.00 sec)

4.連接到主2服務器上面

change master to master_host=10.220.5.138,master_user=ken,master_password=‘xx,master_log_file=mysql-bin.000004,master_log_pos=1558;

5.啟動slave

mysql> start slave;

6.檢查連接狀態

mysql> show slave status\G
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    8
Current database: *** NONE ***

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.220.5.138
                  Master_User: ken
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 1558
               Relay_Log_File: relay-bin.000004
                Relay_Log_Pos: 602
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes    <<IO線程啟動成功
            Slave_SQL_Running: Yes    <<SQL線程啟動成功
              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: 1558
              Relay_Log_Space: 803
              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: 1003307
                  Master_UUID: c01b1811-d7b3-11e8-8698-000c29492f7b
             Master_Info_File: /data/mysql/mysql3306/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: c01b1811-d7b3-11e8-8698-000c29492f7b:3
            Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-5,
c01b1811-d7b3-11e8-8698-000c29492f7b:3
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.01 sec)

主1節點配置完畢,並且啟動成功,下面來配置主2服務器。

1.修改主2配置文件

1 #my.cnf
  2 [client]
  3 port            = 3306
  4 socket          = /tmp/mysql3306.sock
  5 
  6 [mysql]
  7 #prompt="\\u@\\h:\\p [\\d]>" 
  8 #pager="less -i -n -S"
  9 #tee=/opt/mysql/query.log
 10 no-auto-rehash
 11 
 12 [mysqld]
 13 #misc
 14 user = mysql
 15 basedir = /usr/local/mysql
 16 datadir = /data/mysql/mysql3306/data
 17 port = 3306
 18 socket = /tmp/mysql3306.sock
 19 event_scheduler = 0
 20 #skip-grant
 21 
 22 tmpdir = /data/mysql/mysql3306/tmp
 23 #timeout
 24 interactive_timeout = 300
 25 wait_timeout = 300
 26 #auto_increment_offset = 2
 27 #auto_increment_increment = 2
 28 
 29 #character set
 30 character-set-server = utf8
 32 open_files_limit = 65535
 33 max_connections = 100
 34 max_connect_errors = 100000
 35 lower_case_table_names =1
 36 auto_increment_offset=2         <<從2開始增長
 37 auto_increment_increment=2       <<每次增長為2,即為偶數
 38 
 39 #symi replication
 40 
 41 #rpl_semi_sync_master_enabled=1
 42 #rpl_semi_sync_master_timeout=1000 # 1 second
 43 #rpl_semi_sync_slave_enabled=1
 44 
 45 #logs
 46 log-output=file
 47 slow_query_log = 1
 48 slow_query_log_file = slow.log
 49 log-error = error.log
 50 log_warnings = 2
 51 pid-file = mysql.pid
 52 long_query_time = 1
 53 #log-slow-admin-statements = 1
 54 #log-queries-not-using-indexes = 1
 55 log-slow-slave-statements = 1
 56 
 57 #binlog
 58 #binlog_format = STATEMENT
 59 binlog_format = row
 60 server-id = 1003307   <<兩個服務器的server_id必須不一致
61 log-bin = /data/mysql/mysql3306/logs/mysql-bin
 62 binlog_cache_size = 4M
 63 max_binlog_size = 256M
 64 max_binlog_cache_size = 1M
 65 sync_binlog = 0
 66 expire_logs_days = 10
 67 #procedure 
 68 log_bin_trust_function_creators=1
 69 
 70 #
 71 gtid-mode = on
 72 enforce-gtid-consistency=1
 73 
 74 
 75 #relay log
 76 skip_slave_start = 1
 77 max_relay_log_size = 128M
 78 relay_log_purge = 1
 79 relay_log_recovery = 1
 80 relay-log=relay-bin
 81 relay-log-index=relay-bin.index
 82 log_slave_updates
 83 #slave-skip-errors=1032,1053,1062
 84 #skip-grant-tables
 85 
 86 #buffers & cache
 87 table_open_cache = 2048
 88 table_definition_cache = 2048
 89 tabl90 max_heap_table_size = 96M
 91 sort_buffer_size = 128K
 92 join_buffer_size = 128K
 93 thread_cache_size = 200
 94 query_cache_size = 0
 95 query_cache_type = 0
 96 query_cache_limit = 256K
 97 query_cache_min_res_unit = 512
 98 thread_stack = 192K
 99 tmp_table_size = 96M
100 key_buffer_size = 8M
101 read_buffer_size = 2M
102 read_rnd_buffer_size = 16M
103 bulk_insert_buffer_size = 32M
104 
105 #myisam
106 myisam_sort_buffer_size = 128M
107 myisam_max_sort_file_size = 10G
108 myisam_repair_threads = 1
109 
110 #innodb
111 innodb_buffer_pool_size = 100M
112 innodb_buffer_pool_instances = 1
113 innodb_data_file_path = ibdata1:100M:autoextend
114 innodb_flush_log_at_trx_commit = 2
115 innodb_log_buffer_size = 8M
116 innodb_log_file_size = 100M
117 innodb_log_files_in_group = 3
118 innodb_max_dirty_pages_pct = 50
119 innodb_file_per_table = 1e_open_cache = 2048
120 innodb_rollback_on_timeout
121 innodb_status_file = 1
122 innodb_io_capacity = 100
123 transaction_isolation = READ-COMMITTED
124 innodb_flush_method = O_DIRECT

2.啟動mysql並創建復制用的用戶

[root@ken ~]# mysqld &              <<啟動mysql
[4] 2896 
[root@ken ~]# mysql -uroot -p       <<登錄mysql
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> grant replication slave on *.* to ken@‘%‘ identified by ‘xx‘;  <<創建復制用的用戶

3.檢查主1的二進制日誌文件位置,在主1服務器上面執行下面的命令獲取到當前二進制日誌記錄位置

mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000009
Position: 1526
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-5,
c01b1811-d7b3-11e8-8698-000c29492f7b:3
1 row in set (0.01 sec)

4.連接到主2服務器上面

change master to master_host=‘10.220.5.137‘,master_user=‘ken‘,master_password=‘xx‘,master_log_file=‘mysql-bin.000009‘,master_log_pos=1526;

5.啟動slave

mysql> start slave;

6.檢查連接狀態

mysql> show slave status\G
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    8
Current database: *** NONE ***

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.220.5.137
                  Master_User: ken
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000009
          Read_Master_Log_Pos: 1558
               Relay_Log_File: relay-bin.000009
                Relay_Log_Pos: 602
        Relay_Master_Log_File: mysql-bin.000009
             Slave_IO_Running: Yes    <<IO線程啟動成功
            Slave_SQL_Running: Yes    <<SQL線程啟動成功
              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: 1558
              Relay_Log_Space: 803
              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: 1003307
                  Master_UUID: c01b1811-d7b3-11e8-8698-000c29492f7b
             Master_Info_File: /data/mysql/mysql3306/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: c01b1811-d7b3-11e8-8698-000c29492f7b:3
            Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-5,
c01b1811-d7b3-11e8-8698-000c29492f7b:3
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.01 sec)

現在兩個主服務器都已經搭建完,現在來進行測試

1.首先在主1服務器上面建表建庫並插入數據

mysql> create database ken;        <<創建數據庫
mysql> use ken;                    <<切換數據庫
mysql> create table tb2(id int primary key auto_increment,name char(30)); <<創建表
mysql> insert into tb2 (name) values(kk),(kkl),(dad);   <<插入數據
mysql> select * from tb2;     <<檢查數據,為奇數排列
+----+------+
| id | name |
+----+------+
|  1 | kk   |
|  3 | kkl  |
|  5 | dad  |

2.登錄主2服務器並檢查是否已經有主1上面建的表庫

MySQL [ken]> select * from tb2;   <<同步成功
+----+------+
| id | name |
+----+------+
|  1 | kk   |
|  3 | kkl  |
|  5 | dad  |

3.在主2服務器上面插入數據,檢查數據是否會沖突以及同步到主1

MySQL [ken]> insert into tb2 (name) values(lll),(lll),(lll);
MySQL [ken]> select * from tb2;
+----+------+
| id | name |
+----+------+
|  1 | kk   |
|  3 | kkl  |
|  5 | dad  |
|  6 | lll  |             <<偶數排序
|  8 | lll  |
| 10 | lll  |
+----+------+
6 rows in set (0.00 sec)

4.檢查主1服務器是否已經同步過來

mysql> select * from tb2;
+----+------+
| id | name |
+----+------+
|  1 | kk   |
|  3 | kkl  |
|  5 | dad  |
|  6 | lll  |
|  8 | lll  |
| 10 | lll  |
+----+------+
6 rows in set (0.00 sec)

同步成功!這樣就實現了mysql雙主的架構.

MySQL系列詳解七:MySQL實現雙主架構演示-技術流ken