docker(mysql)

2020-11-06 08:00:19 浏览数 (1)

路径

数据库:/usr/local/database

服务器:/usr/local/serverFolder

mysql

代码语言:javascript复制
# 拉取镜像
docker pull mysql:8.0.20
# 启动加挂载
docker run -d -p 3306:3306 -v /usr/local/database/mysql/conf:/etc/mysql/conf.d -v /usr/local/database/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name cxymysql mysql:8.0.20

集群

代码语言:javascript复制
# 创建两个文件夹,用来挂载数据,和配置文件。
# 主节点
[mysqld]
server_id=1
skip-host-cache
skip-name-resolve
#  从节点
[mysqld]
server_id=2
skip-host-cache
skip-name-resolve
# 两个文件主要内容的差异是 server_id 值的不同,必须设置,否则在配置从库时,会报错:
# 主库启动
docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysqlMaster --restart always -v /usr/local/database/mysql/mysqlMaster3306/data:/var/lib/mysql -v /usr/local/database/mysql/mysqlMaster3306:/etc/mysql/conf.d -p 3306:3306 mysql:8.0.20
# 从库启动
docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysqlSlave --restart always -v /usr/local/database/mysql/mysqlSlave3307/data:/var/lib/mysql -v /usr/local/database/mysql/mysqlSlave3307:/etc/mysql/conf.d -p 3307:3306 mysql:8.0.20
# 启动成功,外部测试都可以连接成功
[root@centos8 mysqlSlave3307]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
992d0be96bc1        mysql:8.0.20        "docker-entrypoint.s…"   5 seconds ago       Up 3 seconds        33060/tcp, 0.0.0.0:3307->3306/tcp   mysqlSlave
23ceac64fab5        mysql:8.0.20        "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   mysqlMaster
# 进入主节点内部,查看主机挂载配置文件,没有问题。----------------------------------------------------------------
[root@centos8 mysqlSlave3307]# docker exec -it 23ceac64fab5 /bin/bash
root@23ceac64fab5:/# cd /etc/mysql/conf.d/
root@23ceac64fab5:/etc/mysql/conf.d# ls
data  master.cnf
root@23ceac64fab5:/etc/mysql/conf.d# cat master.cnf 
[mysqld]
server_id=1
skip-host-cache
skip-name-resolve
# 登陆主节点mysql 大p是端口,小p是密码
 mysql -u root -h 127.0.0.1 -P 3306 -p
# 创建用户
# 创建用户,identified with 'mysql_native_password' 表示使用该用户的密码加密方式。
# 赋予用户相应的权限;
# 刷新 MySql 系统权限。
 mysql> create user 'reader'@'%' identified with 'mysql_native_password' by 'reader';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on *.* to 'reader'@'%';
Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
# 查看 master 节点的状态
mysql> show master status;
 --------------- ---------- -------------- ------------------ ------------------- 
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
 --------------- ---------- -------------- ------------------ ------------------- 
| binlog.000002 |      819 |              |                  |                   |
 --------------- ---------- -------------- ------------------ ------------------- 
1 row in set (0.00 sec)
# 进入从节点内部,查看主机挂载配置文件,没有问题。-----------------------------------------------------------------
# 将次节点挂载到主库下面
mysql> CHANGE MASTER TO MASTER_HOST='192.168.106.129',Master_Port=3306,MASTER_USER='reader',MASTER_PASSWORD='reader',MASTER_LOG_FILE='binlog.000002',MASTER_LOG_POS=0;

Query OK, 0 rows affected, 2 warnings (0.05 sec)

# 开启主从同步
mysql> start slave;

Query OK, 0 rows affected (0.06 sec)
# 查看从库状态  Slave_IO_Running: Yes Slave_SQL_Running: Yes 即为成功。
mysql> show slave statusG;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.106.129
                  Master_User: reader
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000002
          Read_Master_Log_Pos: 819
               Relay_Log_File: 992d0be96bc1-relay-bin.000002
                Relay_Log_Pos: 1028
        Relay_Master_Log_File: binlog.000002
             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: 819
              Relay_Log_Space: 1244
              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: 887c1b23-d39c-11ea-ab3b-0242ac110002
             Master_Info_File: mysql.slave_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: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set (0.00 sec)

ERROR: 
No query specified

测试成功

0 人点赞