Linux安装MySQL+Mycat

首先docker启动

[root@localhost ~]# service docker start
Redirecting to /bin/systemctl start docker.service

docker 安装两个数据库并设置主从备份

mkdir db1
mkdir db2

[root@localhost ~]# ll
总用量 40
-rwxrwxrwx. 1 root  root     33 5月  28 09:02 aaa.sh
-rw-------. 1 root  root   1611 5月  23 23:14 anaconda-ks.cfg
drwxr-xr-x. 2 root  root     20 6月  15 10:07 db1
drwxr-xr-x. 2 root  root     20 6月  15 10:09 db2

创建数据库配置文件

vi /root/db1/my.cnf

插入

[mysqld]
## 同一局域网内注意要唯一
server-id=100  
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin

vi /root/db2/my.cnf

插入

[mysqld]
## 设置server_id,注意要唯一
server-id=101  

## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin

## relay_log配置中继日志
relay_log=edu-mysql-relay-bin

拉取镜像并创建数据库

[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
Digest: sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa
Status: Image is up to date for mysql:5.7
docker.io/library/mysql:5.7

[root@localhost ~]# docker run --name db1 -p 3316:3306 -v /root/db1/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

f52dce960ecba5e1496e926fed00afe0e8cd5a8fe49659d958932a3362e56eec

[root@localhost ~]# docker run --name db2 -p 3326:3306 -v /root/db2/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
d27b354fd6b10a3098f0a84f24465e1c39656c4c81fa386344ce22a5b8ea2be2

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED              STATUS              PORTS                                                  NAMES
d27b354fd6b1   mysql:5.7   "docker-entrypoint.s…"   57 seconds ago       Up 47 seconds       33060/tcp, 0.0.0.0:3326->3306/tcp, :::3326->3306/tcp   db2
f52dce960ecb   mysql:5.7   "docker-entrypoint.s…"   About a minute ago   Up About a minute   33060/tcp, 0.0.0.0:3316->3306/tcp, :::3316->3306/tcp   db1

打开db1的数据库进行查看

[root@localhost ~]# docker exec -it db1 /bin/bash

root@f52dce960ecb:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

查看db1的端口号地址

[root@localhost ~]# docker inspect db1

                  
                    "IPAddress": "172.17.0.2"

安装在宿主机上安装mysql客户端,测试连通性

[root@localhost ~]# yum install -y mysql 
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * epel: mirrors.neusoft.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 mariadb.x86_64.1.5.5.68-1.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
=======================================================================================
 Package            架构              版本                       源               大小
=======================================================================================
正在安装:
 mariadb            x86_64            1:5.5.68-1.el7             base            8.8 M
事务概要
=======================================================================================
安装  1 软件包

总下载量:8.8 M
安装大小:49 M
Downloading packages:
mariadb-5.5.68-1.el7.x86_64.rpm                                 | 8.8 MB  00:00:06     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : 1:mariadb-5.5.68-1.el7.x86_64                                      1/1 
  验证中      : 1:mariadb-5.5.68-1.el7.x86_64                                      1/1 
已安装:
  mariadb.x86_64 1:5.5.68-1.el7                                                        
完毕!

[root@localhost ~]# mysql -uroot -p123456 -h 172.17.0.2
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.34-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> exit

配置主从复制

在Master数据库创建数据同步用户,授予用户 wqr123 REPLICATION SLAVE权限和REPLICATION CLIENT权限,用于在主从库之间同步数据。
通过docker exec -it db1 /bin/bash命令进入到Master容器内部执行mysql客户端执行如下脚本。

mysql> CREATE USER 'wqr123'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.03 sec)

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'wqr123'@'%';
Query OK, 0 rows affected (0.01 sec)

在Master(db1)进入mysql,执行show master status;

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

退出master(db1)数据库,回到宿主机执行docker命令在Slave(db2) 中进入 mysql,执行

change master to master_host=‘172.17.0.2’, master_user=‘wqr123’, master_password=‘123456’, master_port=3306,
master_log_file=‘mysql-bin.000003’, master_log_pos= 619, master_connect_retry=30;
命令说明:
master_host :Master的地址,指的是容器的独立ip,可以通过如下命令查询容器的ip
docker inspect --format=’{{.NetworkSettings.IPAddress}}’ 容器名称|容器id
Copy to clipboardErrorCopied
master_port:Master的端口号,指的是容器的端口号
master_user:用于数据同步的用户
master_password:用于同步的用户的密码
master_log_file:指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值
master_log_pos:从哪个 Position 开始读,即上文中提到的 Position 字段的值
master_connect_retry:如果连接失败,重试的时间间隔,单位是秒,默认是60秒

mysql> exit
Bye
root@f52dce960ecb:/# exit
exit

[root@localhost ~]# docker exec -it db2 /bin/bash
root@d27b354fd6b1:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> change master to master_host='172.17.0.2',  master_user='wqr123', master_password='123456', master_port=3306,  
    -> master_log_file='mysql-bin.000003', master_log_pos= 619,  master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.22 sec)

在Slave(db2) 中的mysql终端执行show slave status \G;用于查看主从同步状态。

mysql>  show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 172.17.0.2
                  Master_User: wqr123
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 619
               Relay_Log_File: edu-mysql-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: No
            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: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 619
              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: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           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)

ERROR: 
No query specified

正常情况下,SlaveIORunning 和SlaveSQLRunning 都是No,因为我们还没有开启主从复制过程。使用start slave开启主从复制过程,然后再次查询主从同步状态show slave status \G;。

mysql>  start slave ;
Query OK, 0 rows affected (0.02 sec)

mysql>  show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.17.0.2
                  Master_User: wqr123
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 619
               Relay_Log_File: edu-mysql-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             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: 619
              Relay_Log_Space: 531
              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: 100
                  Master_UUID: 89cec3ad-cd7f-11eb-8755-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 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)

ERROR: 
No query specified

SlaveIORunning 和 SlaveSQLRunning 都是Yes,说明主从复制已经开启。此时可以测试数据同步是否成功。

dm sql与mysql的区别_linux


dm sql与mysql的区别_docker_02