Docker安装部署mysql
下载镜像
docker pull mysql:5.7.35
查看镜像
docker images
启动
直接启动不挂载文件
docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.35
挂载文件
docker run -p 3306:3306 --name mysql \
-v /usr/local/docker/mysql/conf:/etc/mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7.35
- -p 指定端口
- –name 指定容器名称
- -d 后台运行
- -e 指定参数,这里是数据库密码,默认用户名为root
- -v 指定数据卷
查看容器
docker container ls
主库设置
my.cnf配置更改
[mysqld]
log_bin = mysql-bin #记录操作日志
binlog_ignore_db = mysql #不同步 mysql 系统数据库
server_id = 30 #数据库集群中的每个节点 id 都要不同,一般使用 IP 地址的最后段的数字,例如 192.168.200.30,server_id 就写 30
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
重启mysql
docker ps
docker container stop xxxxxx
docker rm xxxxxxxx
停掉容器删除容器之后继续执行docker run如上,此处不再赘述。
创建slave用户
注意:一般不会使用root用户,而是创建一个同步的用户!
grant replication slave on *.* to '用户名'@'ip或者%' identified by '密码';
创建完用户后,登录验证。
查询master信息
show master status;
从库设置
my.cnf配置更改
[mysqld]
log_bin = mysql-bin #记录操作日志
binlog_ignore_db = mysql #不同步 mysql 系统数据库
server_id = 40 #数据库集群中的每个节点 id 都要不同,一般使用 IP 地址的最后段的数字,例如 192.168.200.40,server_id 就写40
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
重启mysql
docker ps
docker container stop xxxxxx
docker rm xxxxxxxx
停掉容器删除容器之后继续执行docker run如上,此处不再赘述。
从库设置主库
change master to master_host='master机器ip',master_user='master用户名',master_password='master密码',master_log_file='File',master_logs_pos=Position;
master_log_file=‘File’,master_logs_pos=Position两个参数是主节点执行show master status查询时的数据。
启动slave
start slave;
查看同步状态
show slave status;