首页 > 数据库 >centos7 用docker搭建Mysql主从

centos7 用docker搭建Mysql主从

时间:2023-07-11 16:14:16浏览次数:43  
标签:compose Log Mysql centos7 Master mysql MASTER docker

安装Docker和Docker Compose:

安装预置:
sudo yum update
yum install python3-pip
sudo yum group install "Development Tools"
sudo yum install epel-release
sudo yum install openssl-devel libffi-devel python3-devel

安装docker:
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
ls /var/lib/docker
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
systemctl start docker


安装docker-compose :
sudo rm /usr/local/bin/docker-compose (删除旧的)
sudo curl -L "https://developer.aliyun.com/compose/docker-compose/$(uname -s)/$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker compose version (验证版本,docker-compose 这个旧格式不能用了)

[root@localhost MYSQL]# docker compose version
Docker Compose version v2.19.1

 

创建容器:
docker compose up -d (docker-compose 这个旧格式不能用了)

 

配置主库(Mysql):

docker exec -it [容器ID或名称] bash

mysql -uroot -p

mysql> CREATE USER 'replication_user'@'%' IDENTIFIED BY 'replication_password';
Query OK, 0 rows affected (0.01 sec)

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

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW MASTER STATUS; # 这里注意MASTER_LOG_POS = 881,要在从节点配置。
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 | 881 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

 

 

配置从库(Slave):

docker exec -it [容器ID或名称] bash

mysql -uroot -p

mysql> STOP SLAVE;

mysql> CHANGE MASTER TO
-> MASTER_HOST = 'mysql-master',
-> MASTER_PORT = 3306,
-> MASTER_USER = 'replication_user',
-> MASTER_PASSWORD = 'replication_password',
-> MASTER_LOG_FILE = 'binlog',
-> MASTER_LOG_POS = 881; #刚才在主节点【SHOW MASTER STATUS;】获取的值。
Query OK, 0 rows affected, 9 warnings (0.01 sec)

mysql> START SLAVE; # 启动从库的复制进程:
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> SHOW SLAVE STATUS \G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to source
Master_Host: mysql-master
Master_User: replication_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog
Read_Master_Log_Pos: 881
Relay_Log_File: d7adf7f88efa-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: binlog
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: 881
Relay_Log_Space: 157
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: 2061
Last_IO_Error: Error connecting to source 'replication_user@mysql-master:3306'. This was attempt 2/86400, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 230711 07:37:56
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, 1 warning (0.00 sec)

ERROR:
No query specified


确保Slave_IO_Running和Slave_SQL_Running都显示为Connecting to source 和 Yes。如果是No,则可能有配置问题。

根据 MySQL SHOW SLAVE STATUS \G 的输出,可以看到以下相关信息:
Read_Master_Log_Pos: 881
Exec_Master_Log_Pos: 881
这表示当前从服务器(Slave)正在读取主服务器(Master)的二进制日志文件,并且已经读取到位置为 881 的位置。

标签:compose,Log,Mysql,centos7,Master,mysql,MASTER,docker
From: https://www.cnblogs.com/sinsenliu/p/17544994.html

相关文章

  • docker pull拉 x509: certificate has expired or is not yet valid:
    [[email protected]]#dockerpullXXXcode-analysis:centos7.3-1.0Errorresponsefromdaemon:Get"XXXX":x509:certificatehasexpiredorisnotyetvalid:currenttime2023-07-11T14:27:37+08:00isafter2018-08-22T11:54:50Z x509:c......
  • 编译安装MySQL8及常见错误处理
    初稿。。。1.下载https://downloads.mysql.com/archives/community/选择对应版本->选择sourcecode->系统版本一定要选all->拉至最下方-选择mysql-boost-8.0.xx.tar.gz(不下载带有boost的压缩包,需自行安装boost)图片后续添加。 2.解压tar-zxfmysql-boost-8.0.25.tar.gz......
  • python pandas链接mysql数据库报错
    1. ModuleNotFoundError:Nomodulenamed'mysql'需要安装链接驱动,比如 pipinstallmysqlclient#或pipinstallpymysql#或pipinstallmysql-connector-python2.pandas的 pd.read_sql(ad_first_query,conn)报错: executereturnself.connectable.executio......
  • docker部署mssql
    docker部署SQLServer2022参考微软官方文档Docker:为Linux上的SQLServer安装容器-SQLServer|MicrosoftLearn(安装的是开发人员版)1.请求映像dockerpullmcr.microsoft.com/mssql/server:2022-latest注意:server:xx,xx表示对应的版本(https://hub.docker.com/r/mi......
  • centos7的bond负载均衡(2)
    bond绑定环境:centos7.9,两个网卡添加网卡:添加配置文件件[root@localhostnetwork-scripts]#catifcfg-ens33TYPE=EthernetPROXY_METHOD=noneBROWSER_ONLY=noBOOTPROTO=staticDEFROUTE=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_FAILURE_FATAL=......
  • mysql - docker主从复制
    1、docker拉取镜像dockerpullmysql:8.0 2、查看dockerimages 3、创建容器主:dockerrun-p3309:3306--namemaster-eMYSQL_ROOT_PASSWORD=123456--restart=always-dmysql:8.0 从:dockerrun-p3310:3306--namesalve-eMYSQL_ROOT_PASSWORD=123456--res......
  • (转)Docker格式化输出命令:"docker inspect --format" 学习笔记
    原文:https://www.cnblogs.com/kevingrace/p/6424476.htmlDocker--format参数提供了基于Go模板的日志格式化输出辅助功能,并提供了一些内置的增强函数。什么是模板?上图是大家熟悉的 MVC框架(ModelViewController): Model(模型,通常在服务端)用于处理数据、View(视图,客户端代码......
  • mysql8.0多实例部署,同版本多实例和不同版本实例
    一、配置Mysql8.0同版本多实例1、解压移动安装位置2、创建Mysql管理组和用户3、创建多实例用户数据库存储目录修改目录所有者4、修改多实例Mysql主配置文件5、初始化多实例Mysql6、启动服务7、登录多实例Mysql二、配置Mysql不同版本多实例1、安装Mysql5.62、修改mysql5.6主配置文件......
  • Kubernets与Docker的故事
    在2016年底的1.5版里,Kubernetes引入了一个新的接口标准:CRI,ContainerRuntimeInterface。CRI采用了ProtoBuffer和gPRC,规定kubelet该如何调用容器运行时去管理容器和镜像,但这是一套全新的接口,和之前的Docker调用完全不兼容。 Kubernetes也只能同时提供一个“折中”......
  • optimize table 治理mysql 高水位和空间碎片
    问题背景随着系统上线时间的增长,mysql数据库的数据目录的文件系统使用率已经快要耗尽。从最初的两三个月truancate一次日志表,到最后每周要truancate多次日志表。业务侧将数据库中历史数据(超过全部数据的2/3)逐渐进行了迁移(采用目标库insert,源库delete的方式),这些表中有多个超过1......