MoHA 是 Mobike 开源的跨数据中心的 MySQL 集群的高可用(HA)解决方案
MoHA 提供了详细的设计和使用说明文档,点击查看
启动使用
下载项目
| 1
 | git clone https://gitee.com/piao/moha.git $GOPATH/src/github.com/moiot/moha
 | 
 
可选择修改配置中的账号密码,下面演示未修改,使用默认值
编译项目
| 1
2
3
 | cd $GOPATH/src/github.com/moiot/moha
make docker-agent
 | 
 
编译依赖Docker,需要先安装启动Docker
启动集群
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 | github.com/moiot/moha/etc/docker-compose git:(master) ✗ > docker-compose -f docker-compose.yml up -d
Creating network "docker-compose_default" with the default driver
Creating volume "docker-compose_mysql-node-3-data" with default driver
Creating etcd-node-2    ... done
Creating haproxy-slave  ... done
Creating haproxy-master ... done
Creating etcd-node-1    ... done
Creating etcd-node-3    ... done
Creating etcd-node-0    ... done
Creating etcd-node-4    ... done
Creating pmm-server     ... done
Creating mysql-node-2   ... done
Creating mysql-node-1   ... done
Creating mysql-node-3   ... done
 | 
 
查看运行状态
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 | github.com/moiot/moha/etc/docker-compose git:(master) ✗ > docker-compose -f docker-compose.yml ps
     Name                   Command               State                                                   Ports
---------------------------------------------------------------------------------------------------------------------------------------------------------------
etcd-node-0      /opt/bitnami/scripts/etcd/ ...   Up      0.0.0.0:32768->2379/tcp, 2380/tcp
etcd-node-1      /opt/bitnami/scripts/etcd/ ...   Up      0.0.0.0:32769->2379/tcp, 2380/tcp
etcd-node-2      /opt/bitnami/scripts/etcd/ ...   Up      0.0.0.0:32770->2379/tcp, 2380/tcp
etcd-node-3      /opt/bitnami/scripts/etcd/ ...   Up      0.0.0.0:32771->2379/tcp, 2380/tcp
etcd-node-4      /opt/bitnami/scripts/etcd/ ...   Up      0.0.0.0:32772->2379/tcp, 2380/tcp
haproxy-master   /docker-entrypoint.sh hapr ...   Up      0.0.0.0:5000->5000/tcp, 0.0.0.0:7000->7000/tcp
haproxy-slave    /docker-entrypoint.sh hapr ...   Up      0.0.0.0:5001->5000/tcp, 0.0.0.0:7001->7000/tcp
mysql-node-1     /agent/supervise                 Up      0.0.0.0:13307->13306/tcp, 0.0.0.0:3307->3306/tcp
mysql-node-2     /agent/supervise                 Up      0.0.0.0:13308->13306/tcp, 0.0.0.0:3308->3306/tcp
mysql-node-3     /agent/supervise                 Up      0.0.0.0:13309->13306/tcp, 0.0.0.0:3309->3306/tcp
pmm-server       /opt/entrypoint.sh               Up      0.0.0.0:3000->3000/tcp, 443/tcp, 0.0.0.0:8080->80/tcp, 0.0.0.0:8500->8500/tcp, 0.0.0.0:9090->9090/tcp
 | 
 
浏览器访问监控数据
http://localhost:8080/
查看etcd目录
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 | > docker exec -it etcd-node-0 /bin/sh -c "etcdctl get / --prefix --keys-only"
/dbproxy/clustername/ks_cfg/nodes/node-test/election/master/id
/dbproxy/clustername/ks_cfg/nodes/node-test/election/master/last_gtid
/dbproxy/clustername/ks_cfg/nodes/node-test/election/master/last_txnid
/dbproxy/clustername/ks_cfg/nodes/node-test/election/master/last_uuid
/dbproxy/clustername/ks_cfg/nodes/node-test/election/master/meta_version
/dbproxy/clustername/ks_cfg/nodes/node-test/election/master/term
/dbproxy/clustername/ks_cfg/nodes/node-test/election/terms/127.0.0.1:3307
/dbproxy/clustername/ks_cfg/nodes/node-test/election/terms/127.0.0.1:3308
/dbproxy/clustername/ks_cfg/nodes/node-test/election/terms/127.0.0.1:3309
/dbproxy/clustername/ks_cfg/nodes/node-test/master
/dbproxy/clustername/ks_cfg/nodes/node-test/slave/127.0.0.1:3307
/dbproxy/clustername/ks_cfg/nodes/node-test/slave/127.0.0.1:3308
 | 
 
查看mysql集群主节点
| 1
2
3
 | > docker exec -it etcd-node-0 /bin/sh -c "etcdctl get /dbproxy/clustername/ks_cfg/nodes/node-test/master"
/dbproxy/clustername/ks_cfg/nodes/node-test/master
127.0.0.1:3307
 | 
 
访问mysql集群主节点
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
 | > docker exec -it mysql-node-1 /bin/bash
root@9936e9af59be:/# mysql -umysql_user -pmysql_master_user_pwd
[mysql_user@localhost][checker]> show grants;
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for mysql_user@%                                                                                               |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, RELOAD, SUPER, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'mysql_user'@'%' |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
 | 
 
使用root账号访问mysql集群主节点
| 1
2
3
4
5
6
7
 | CREATE TABLE IF NOT EXISTS `mo_tbl`(
   `id` INT UNSIGNED AUTO_INCREMENT,
   `title` VARCHAR(100) NOT NULL,
   `author` VARCHAR(40) NOT NULL,
   `date` DATE,
   PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
 | 
 
创建数据表并写入数据
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 | > docker exec -it mysql-node-1 /bin/bash
root@9936e9af59be:/# mysql -uroot -pmaster_root_pwd
[root@localhost][checker]> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
[root@localhost][checker]> CREATE TABLE IF NOT EXISTS `mo_tbl`(
    ->    `id` INT UNSIGNED AUTO_INCREMENT,
    ->    `title` VARCHAR(100) NOT NULL,
    ->    `author` VARCHAR(40) NOT NULL,
    ->    `date` DATE,
    ->    PRIMARY KEY ( `id` )
    -> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.03 sec)
[root@localhost][checker]> desc mo_tbl;
+--------+------------------+------+-----+---------+----------------+
| Field  | Type             | Null | Key | Default | Extra          |
+--------+------------------+------+-----+---------+----------------+
| id     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title  | varchar(100)     | NO   |     | NULL    |                |
| author | varchar(40)      | NO   |     | NULL    |                |
| date   | date             | YES  |     | NULL    |                |
+--------+------------------+------+-----+---------+----------------+
4 rows in set (0.02 sec)
[root@localhost][checker]> insert into mo_tbl (title, author, date) values ("test", "piao", "2021-09-04");
Query OK, 1 row affected (0.01 sec)
[root@localhost][checker]> select * from mo_tbl;
+----+-------+--------+------------+
| id | title | author | date       |
+----+-------+--------+------------+
|  1 | test  | piao   | 2021-09-04 |
+----+-------+--------+------------+
1 row in set (0.00 sec)
 | 
 
参考