创建本地映射目录
[root@VM-4-16-centos data]# mkdir -p /data/mysql/{data,logs,conf}
[root@VM-4-16-centos data]#
[root@VM-4-16-centos data]# chmod 777 -R /data/mysql/logs/
[root@VM-4-16-centos data]#
[root@VM-4-16-centos data]# cd mysql/
[root@VM-4-16-centos mysql]# ls
conf data logs
[root@VM-4-16-centos mysql]# pwd
/data/mysql
[root@VM-4-16-centos mysql]#
创建mysql配置文件
[root@VM-4-16-centos mysql]# cd conf/
[root@VM-4-16-centos conf]# vim mysql.cnf
[root@VM-4-16-centos conf]#
[root@VM-4-16-centos conf]# cat mysql.cnf
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
#skip-grant-tables
#skip-host-cache
#skip-name-resolve
#datadir=/var/lib/mysql
datadir=/data/mysql/data
#socket=/var/lib/mysql/mysql.sock
socket=/data/mysql/data/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names=1
#secure-file-priv=/var/lib/mysql-files
#user=mysql
#pid-file=/var/run/mysqld/mysqld.pid
#server-id=1
character-set-server=UTF8MB4
## 指定不需要同步的数据库名称
#binlog-ignore-db=mysql
## 开启二进制日志功能
#log-bin=mall-mysql-bin
## 设置二进制日志使用内存大小(事务)
#binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
#binlog_format=mixed
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
#expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
#slave_skip_errors=1062
[client]
socket=/data/mysql/data/mysql.sock
拉取mysq镜像
(本次以mysql5.7为例)
[root@VM-4-16-centos conf]# docker pull mysql:5.7
5.7: Pulling from library/mysql
70e9ff4420fb: Pull complete
7ca4383b183f: Pull complete
3e282e7651b1: Pull complete
1ffa0e0ca707: Pull complete
6eb790cf6382: Pull complete
b4b277ff2929: Pull complete
692fe4469429: Pull complete
c0d447d97bbd: Pull complete
99ee594517ba: Pull complete
a9ae52de4d77: Pull complete
66cc05a182b5: Pull complete
Digest: sha256:2c23f254c6b9444ecda9ba36051a9800e8934a2f5828ecc8730531db8142af83
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@VM-4-16-centos conf]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 92034fe9a41f 5 weeks ago 581MB
[root@VM-4-16-centos conf]#
创建mysql容器
[root@VM-4-16-centos conf]# docker run -d --restart=always --name="mysql" -p 3306:3306 -v /data/mysql/conf/:/etc/mysql/conf.d -v /data/mysql/data:/var/lib/mysql -v /data/mysql/logs:/var/log -e MYSQL_ROOT_PASSWORD="123456" mysql:5.7
003884a5be5ca944e5346979f19834a98679ed3893d5a650025f7cab21c965f9
[root@VM-4-16-centos conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
003884a5be5c mysql:5.7 "docker-entrypoint.s…" 7 minutes ago Up 56 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
登录验证
[root@VM-4-16-centos conf]# docker exec -it mysql /bin/bash
bash-4.2# mysql -h 127.0.0.1 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, 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>