获取镜像
# 查看可用的稳定版本
sudo docker search mysql
sudo docker pull mysql:5.7.23
sudo docker image ls |grep mysql
服务编排
mkdir -p /share/mysql
vi /share/mysql/docker-compose.yml
服务编排内容如下:
version: "2"
networks:
mysqlnet:
services:
mysqldb:
build: ./mysql
container_name: mysql.db
user: "1000:50"
networks:
- mysqlnet
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: '123456'
volumes:
- ./mysql/data:/var/lib/mysql
- ./mysql/conf/my.cnf:/etc/my.cnf
- ./mysql/init:/docker-entrypoint-initdb.d/
command:
--default-authentication-plugin=mysql_native_password
--socket=/tmp/mysql.sock
服务配置
Φ 配置文件概览
以下配置相关文件保持与编排文件相同目录。
d conf # 初始化配置
- my.cnf # MySQL配置文件
d data # MySQL持久化数据
d init # 初始化脚本
- init.sql # 初始化库表、初始化数据等
- Dockerfile # 镜像制作文件
- privileges.sql # MySQL权限配置
- setup.sh # 启动逻辑控制脚本(entrypoint)
Φ Dockerfile
FROM mysql:5.7.23
# 设置免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
# 新增用户和密码
ENV MYSQL_USER=db2odesk
ENV MYSQL_PASSWORD=531.Desk
# 将所需文件放到容器中
COPY setup.sh /mysql/setup.sh
COPY privileges.sql /mysql/privileges.sql
RUN chown -R mysql:mysql /var/lib/mysql /var/run/mysqld /mysql /tmp
RUN chmod -R 777 /var/lib/mysql /var/run/mysqld /mysql /tmp
RUN ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
VOLUME /var/lib/mysql
# 设置工作目录
WORKDIR /mysql
EXPOSE 3306 33060
# 设置容器启动时执行的命令
# ENTRYPOINT ["sh", "/mysql/setup.sh"]
CMD ["sh", "/mysql/setup.sh"]
Φ my.cnf
[mysqld]
user=mysql
datadir=/var/lib/mysql
pid-file=/var/run/mysqld/mysqld.pid
socket=/tmp/mysql.sock
lower_case_table_names=2
port=3306
default-storage-engine=INNODB
character-set-server=utf8
explicit_defaults_for_timestamp=true
skip-external-locking
innodb_flush_method=O_DSYNC
default-time_zone = '+8:00'
[client]
default-character-set=utf8
socket=/tmp/mysql.sock
[mysql]
default-character-set=utf8
socket=/tmp/mysql.sock
[mysqld_safe]
core-file-size=10485760
open-files-limit=10000
Φ setup.sh
#!/bin/bash
set -e
# 查看mysql服务的状态,方便调试,这条语句可以删除
echo $(service mysql status)
echo '1.启动mysql....'
# 启动mysql
service mysql start
sleep 5
# 重新设置mysql密码
echo '2.开始密码及权限设置密码....'
mysql -uroot -p123456 --socket=/tmp/mysql.sock < /mysql/privileges.sql
echo '3.修改密码及权限设置完毕....'
sleep 5
echo $(service mysql status)
echo 'mysql容器启动完毕,且数据导入成功'
tail -f /dev/null
Φ privileges.sql
use mysql;
-- 创建新用户并赋权限
-- select host, user from user;
-- create user db2odesk identified by '531.Desk';
-- grant all on db2odesk.* to db2odesk@'%' identified by '531.Desk' with grant option;
-- flush privileges;
-- 配置远程连接支持
update user set host = '%' where user = 'root';
update user set authentication_string=password("123456") where user="root";
update user set authentication_string=password("123456"),plugin='mysql_native_password' where user='root';
grant all on *.* to root@'%' identified by '123456' with grant option;
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
flush privileges;
alter user 'root'@'%' identified with mysql_native_password by '123456';
flush privileges;
Φ init.sql
-- 创建数据库
create database `deskdb` default character set utf8 collate utf8_general_ci;
use deskdb;
-- 建表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL,
`created_at` bigint(40) DEFAULT NULL,
`last_modified` bigint(40) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`first_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
`username` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- 插入数据
INSERT INTO `user` (`id`, `created_at`, `last_modified`, `email`, `first_name`, `last_name`, `username`) VALUES (0,1490257904,1490257904,'wangjun@lonton.com','joe','Doe','user');
服务构建
cd /share/mysql
sudo docker-compose -f docker-compose.yml build --no-cache # 不带缓存构建(只创建镜像,不会启动容器)
sudo docker-compose -f docker-compose.yml up -d # 构建后运行
sudo docker-compose -f docker-compose.yml up --build # 跟踪方式构建,可用于调试
验证
cd /share/mysql
# 查看进程
sudo docker-compose -f docker-compose.yml ps
# 进入MySQL终端
sudo docker exec -it mysql.db /bin/bash
mysql -u root -p
# 查看日志
sudo docker logs mysql.db
sudo docker logs -f -t --tail=50 mysql.db
# 查看网络
sudo docker network ls
sudo docker inspect mysql_mysqlnet
# 停止
sudo docker-compose -f docker-compose.yml stop
# 移除
sudo docker-compose -f docker-compose.yml down