mysql hma 分布式_mysql基础之mariadb集群双主(主主)架构
一、概念
在企業中,數據庫高可用一直是企業的重中之重,中小企業很多都是使用mysql主從方案,一主多從,讀寫分離等,但是單主存在單點故障,從庫切換成主庫需要作改動。因此,如果是雙主或者多主,就會增加mysql入口,增加高可用。不過多主需要考慮自增長ID問題,這個需要特別設置配置文件,比如雙主,可以使用奇偶,總之,主之間設置自增長ID相互不沖突就能完美解決自增長ID沖突問題。
單點故障解決方案:
主主架構:
互為主備,互相監控對方二進制日志文件進行同步
note:當兩個sql語句發生沖突的時候主主架構有可能出現數據不一致的現象;
MHA(master high availability):
HMA可以有多個配置文件,一個配置文件監控一個主從架構
二、主主架構思路
1、兩臺mysql都可讀可寫,互為主備,默認只使用一臺(masterA)負責數據的寫入,另一臺(masterB)備用;
2、masterA是masterB的主庫,masterB又是masterA的主庫,它們互為主從;
3、兩臺主庫之間做高可用,可以采用keepalived等方案(使用VIP對外提供服務);
4、所有提供服務的從服務器與masterB進行主從同步(雙主多從);
5、建議采用高可用策略的時候,masterA或masterB均不因宕機恢復后而搶占VIP(非搶占模式);
三、演示步驟
環境(主1服務器端IP:192.168.11.7;主2服務器端IP:192.168.11.8)
1、修改配置文件,配置服務器編號,開啟bin-log
[root@bi7 ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=7
log_bin=mysql-bin
binlog_format=row
relay-log=relay-mysql
relay-log-index=relay-mysql.index
log_slave_updates=on
auto-increment-increment=2
auto-increment-offset=1
[root@bi8 ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=8
log_bin=mysql-bin
binlog_format=row
relay-log=relay-mysql
relay-log-index=relay-mysql.index
log_slave_updates=on
auto-increment-increment=2
auto-increment-offset=2
2、重啟mysql服務(兩臺主機)
systemctl restart mariadb
ss -tnl |grep 3306
LISTEN? ? ?0? ? ? 80? ? ? ? ? :::3306? ? ? ? ? ? ? ? ? ? :::*
3、創建復制用的用戶(兩臺主機)
[root@bi8 ~]# mysql -uroot -proot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.26-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the curbit input statement.
MariaDB [(none)]> grant replication slave on *.* to 'master'@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
4、(若第一次同步可忽略此步驟)這里可以清空兩臺主機的master和slave日志(若之前有開啟過同步的,需要先停止同步:stop slave;)
在mysql命令行中輸入:
reset master;
reset slave;
5、查看二進制日志文件的位置
--主1
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 1087 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
--主2
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 520 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
6、為防止配置同步的同時又數據寫入,可給數據庫加讀鎖(兩臺主機):
MariaDB [(none)]> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
7、兩臺服務器互相連接
--主1連接主2
MariaDB [(none)]> change master to master_host='192.168.11.8',master_user='master',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=520;
Query OK, 0 rows affected (0.01 sec)
--主2連接主1
MariaDB [(none)]> change master to master_host='192.168.11.7',master_user='master',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=1087;
Query OK, 0 rows affected (0.05 sec)
8、啟動slave(兩臺主機)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
9、檢查連接狀態(兩臺主機)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.11.7
Master_User: master
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1087
Relay_Log_File: relay-mysql.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: 1087
Relay_Log_Space: 256
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: 2003
Last_IO_Error: error connecting to master 'master@192.168.11.7:3306' - retry-time: 60 maximum-retries: 86400 message: Can't connect to MySQL server on '192.168.11.7' (113 "No route to host")
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
1 row in set (0.00 sec)
10、添加防火墻規則
[root@bi7 ~]# firewall-cmd --add-port=3306/tcp
success
[root@bi7 ~]# firewall-cmd --add-port=3306/tcp --permanent
success
[root@bi8 ~]# firewall-cmd --add-port=3306/tcp
success
[root@bi8 ~]# firewall-cmd --add-port=3306/tcp --permanent
success
11、再次檢查連接狀態(兩臺主機)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.11.8
Master_User: master
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 520
Relay_Log_File: relay-mysql.000002
Relay_Log_Pos: 555
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: 520
Relay_Log_Space: 860
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: 8
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
1 row in set (0.00 sec)
12、測試
--解鎖(兩臺主機)
MariaDB [(none)]> unlock tables;
Query OK, 0 rows affected (0.00 sec)
--查看主1的原本數據庫
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| bi |
+--------------------+
4 rows in set (0.00 sec)
--在主1上創建新庫yang
MariaDB [(none)]> create database if not exists yang default character set utf8;
Query OK, 1 row affected (0.00 sec)
--查看主2是否有新建的數據庫
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| yang |
+--------------------+
4 rows in set (0.10 sec)
--使用yang數據庫
MariaDB [(none)]> use yang;
Database changed
--在主2上創建新表hello,并插入一條數據
MariaDB [yang]> create table hello(id tinyint unsigned primary key auto_increment not null,name varchar(20));
Query OK, 0 rows affected (0.03 sec)
MariaDB [yang]> insert into hello values(0,'李國祥');
Query OK, 1 row affected (0.00 sec)
MariaDB [yang]> select * from hello;
+----+-----------+
| id | name |
+----+-----------+
| 2 | 李國祥 |
+----+-----------+
1 row in set (0.00 sec)
--查看主1是否新插入的數據
MariaDB [(none)]> use yang;
Database changed
MariaDB [yang]> select * from hello;
+----+-----------+
| id | name |
+----+-----------+
| 2 | 李國祥 |
+----+-----------+
1 row in set (0.01 sec)
總結
以上是生活随笔為你收集整理的mysql hma 分布式_mysql基础之mariadb集群双主(主主)架构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux php 执行python,L
- 下一篇: python 金融可视化_用 Pytho