Docker

Docker部署Amoeba+MySQL读写分离

2019-01-06  本文已影响32人  iXiAo9

环境:

Centos:7.5

Docker版本:18.09.0

服务器Master-IP: 192.168.189.128
服务器Slave-IP: 192.168.189.130
服务器Amoeba-IP: 192.168.189.128

读写分离基于MySQL主从环境,所以要先部署MySQL主从

创建MySQL-master容器

docker run --name master -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123 10.30.12.55/docker/mysql:5.6

修改my.cnf,添加如下内容

server-id=1
log-bin=mysql-bin

重启master容器

docker restart master

进入master容器,创建复制权限账号

docker exec -it master /bin/bash

登录到MySQL中

mysql -uroot -p123

mysql> grant replication slave on *.* to 'tom'@'%' identified by '123';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1046 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

在slave服务器创建salve容器

docker run --name slave -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123 10.30.12.55/docker/mysql:5.6

修改my.cnf,添加如下内容

server-id=2
relay-log=mysql-relay

重启slave容器

docker restart slave

进入slave容器

docker exec -it master /bin/bash

登录到MySQL中

mysql -uroot -p123

mysql>stop flave;

mysql> change master to 
    -> master_host='192.168.189.128',
    -> master_user='tom',
    -> master_password='123',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=1046;

mysql>start slave;

查看从状态

mysql> show slave status \G;
*************************** 1\. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.189.128
                  Master_User: tom
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1046
               Relay_Log_File: mysql-relay.000002
                Relay_Log_Pos: 933
        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: 1046
              Relay_Log_Space: 1102
              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: 8f825eae-1092-11e9-8eec-0242ac110002
             Master_Info_File: /var/lib/mysql/master.info
                    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
           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
1 row in set (0.00 sec)

ERROR: 
No query specified

至此主从环境境部署完毕

在MySQL的master容器上给amoeba授权一个拥有读写权限的账号

docker exec -it master /bin/bash

mysql -uroot -p123

mysql> grant all on *.* to 'amoeba'@'%' identified by '123';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

创建amoeba容器

docker run --name amoeba -d -p 8066:8066 10.30.12.55/docker/amoeba

修改amoeba容器的配置文件,配置数据库服务器

将dbServer.xml拷贝出来

docker cp amoeba:/usr/local/amoeba/conf/dbServers.xml ./

编辑此文件

vi dbServers.xml

修改mysql-user配置

<!-- mysql user -->
<property name="user">amoeba</property>

<property name="password">123</property>

修改MySQL的master主机的ip地址

<dbServer name="master"  parent="abstractServer">
        <factoryConfig>
                 <!-- mysql ip -->
                 <property name="ipAddress">192.168.189.128</property>
        </factoryConfig>
</dbServer>

修改mysql的slave主机的ip地址

<dbServer name="server2"  parent="abstractServer">
         <factoryConfig>
                 <!-- mysql ip -->
                 <property name="ipAddress">192.168.189.130</property>
         </factoryConfig>
</dbServer>

保存退出并重启amoeba容器

docker restart amoeba

登录amoeba测试

amoeba默认账号为root,密码为空

mysql -uroot -h192.168.189.128 -P8066

</footer>

上一篇 下一篇

猜你喜欢

热点阅读