docker pull mysql:5.7
docker images
mkdir -p /home/service/mysql/data
mkdir -p /home/service/mysql/conf/my.cnf
[mysqld]
user=mysql
character-set-server=utf8
default_authentication_plugin=mysql_native_password
default-time_zone = '+8:00'
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
docker run -p 3306:3306 --name mysql \
-v /home/service/mysql/data:/var/lib/mysql \
-v /home/service/mysql/conf/my.cnf:/etc/my.cnf \
-e MYSQL_ROOT_PASSWORD=123456\
-e TZ=Asia/Shanghai \
-d mysql
docker run -p 3306:3306 --name mysql -v /home/service/mysql/logs:/logs -v /home/service/mysql/data:/mysql_data -e MYSQL_ROOT_PASSWORD=123456 -d mysql
命令讲解
-p
3306:3306:将容器的3306端口映射到主机的3306端口
-e
MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码
-e
TZ=Asia/Shanghai 修改容器默认时区为上海
-v
=/mysqltest/config/my.cnf:/etc/my.cnf:映射配置文件
-v
=/mysqltest/data:/var/lib/mysql:映射数据目录
注意:不同mysql版本可能配置文件目录和路径不一样,mysql5.7的容器路径为 /etc/mysql/mysql.conf.d/mysqld.cnf。
docker exec -it mysql bash
CREATE USER 'admin'@'%' IDENTIFIED BY '123456';
GRANT ALL ON *.* TO 'admin'@'%';
flush privileges;
docker exec -it mysql /bin/bash
cd /etc/mysql/mysql.conf.d
apt-get update
apt-get install vim
vim mysqld.cnf
default-time_zone = '+8:00'
echo "default-time_zone = '+8:00'" >> mysqld.cnf
-v /etc/localtime:/etc/localtime:ro
show variables like '%time_zone%';