如何部署Docker MySQL实验环境?
- By : Will
- Category : Docker, MySQL & MariaDB
- Tags: docker, MySQL
1 基础知识
– MySQL Server支持Docker框架部署
– MySQL Docker映像专为Linux平台构建,其他平台暂不支持
– MySQL Server Docker映像部署前,系统需先安装Docker
2 最佳实践
2.1 环境配置
2.1.1 系统环境配置
IP Address = 10.168.0.71
Host Name = any
OS = CentOS 7.6 x86_64
2.1.2 软件环境配置
如果你尚未部署Docker,请按照如下文档部署,
https://www.cmdschool.org/archives/2183
2.2 创建数据库实例
2.2.1 下载MySQL服务器Docker镜像
docker pull mysql/mysql-server:5.6
其他版本的下载,请使用以下命令,
docker pull mysql/mysql-server:5.5 docker pull mysql/mysql-server:5.6 docker pull mysql/mysql-server:5.7 docker pull mysql/mysql-server:8.0 docker pull mysql/mysql-server:latest
下载完毕后,你可以使用以下命令查看当前下载的镜像,
docker images
可见如下显示,
REPOSITORY TAG IMAGE ID CREATED SIZE mysql/mysql-server 5.6 8fe1c25ccc38 6 weeks ago 218MB
2.2.2 运行数据库实例
docker run --name=mysql1 -d mysql/mysql-server:5.6
另外,可使用如下参数启动实例,
docker run --name mysql1 -d mysql/mysql-server:5.6 --character-set-server=utf8 --collation-server=utf8_general_ci
启动实力后,可见如下显示,
18682cd5edea248c984c711db6b32123ba07b00c8a1e14fb3a22d577705231dc
可使用如下命令查看正在运行的容器,
docker ps
可见如下显示,
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 18682cd5edea mysql/mysql-server:5.6 "/entrypoint.sh mysq…" About a minute ago Up About a minute (healthy) 3306/tcp mysql1
2.2.3 检查MySQL启动的log
docker logs mysql1
2.3 配置数据库实例
2.3.1 获取初始化密码
docker logs mysql1 2>&1 | grep -i "GENERATED ROOT PASSWORD:"
可见如下输出,
[Entrypoint] GENERATED ROOT PASSWORD: [im4DdeRId3gOM0bAKIBzumJYBr
注:以上密码为“[im4DdeRId3gOM0bAKIBzumJYBr”
2.3.2 从容器内部连接MySQL服务器
docker exec -it mysql1 mysql -uroot -p
然后,我们需要使用如下命令修改root密码才能继续,
alter user 'root'@'localhost' identified by 'rootpassword';
如果有以下错误提示,
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by 'rootpassword'' at line 1
请使用如下命令配置,
set password for 'root'@'localhost'=PASSWORD('rootpassword');
2.3.3 创建授权账号
grant all privileges on *.* to 'root'@'%' identified by "rootpassword"; flush privileges;
2.3.4 查询实例IP
docker inspect mysql1
可见如下显示,
[...] "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "42120d1428611f0efc1cd99fbae49c34db63902d4618786b2aeb286bd3bfa1d6", "EndpointID": "2c97f92fd3de40d1342ae036e634fe8cadfd393c94959a3f3d20ad35f97db459", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:03", "DriverOpts": null } } [...]
注:参数”IPAddress”的声明即实例当前地址
2.3.5 测试登录
mysql -uroot -p -h 172.17.0.2
注:请自行安装mysql客户端
2.4 管理MySQL实例
2.4.1 使用shell管理MySQL服务器
docker exec -it mysql1 bash
可见如下命令提示符,
bash-4.2#
可使用如下命令查看MySQL的数据目录,
bash-4.2# ls /var/lib/mysql
可见如下提示,
auto.cnf ib_logfile0 ib_logfile1 ibdata1 mysql mysql.sock performance_schema test
2.4.2 管理MySQL容器的相关命令
docker start mysql1 docker restart mysql1 docker stop mysql1 docker rm mysql1
参阅文档
=====================
https://dev.mysql.com/doc/mysql-linuxunix-excerpt/5.6/en/linux-installation-docker.html
没有评论