拉去镜像
docker pull mysql:5.7
查看镜像
docker images
结果:
[root@VM_0_2_centos conf]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 84164b03fa2e 3 weeks ago 456MB
[root@VM_0_2_centos conf]#
创建mysql的映射目录
mkdir -p /usr/local/mysql/data /usr/local/mysql/logs /usr/local/mysql/conf
创建配置文件
cd /usr/local/mysql/conf && touch my.cnf
启动容器 并映射刚创建的目录、设置root密码为123456
docker run -p 3306:3306 --name mysql -v /usr/local/mysql/conf:/etc/mysql/conf.d -v /usr/local/mysql/logs:/logs -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
执行结果:
[root@VM_0_2_centos conf]# docker run -p 3306:3306 --name mysql -v /usr/local/mysql/conf:/etc/mysql/conf.d -v /usr/local/mysql/logs:/logs -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
d9fe468f1d89546ada1f0436bca022458418fba4c5061593ac1fb135e6490957
[root@VM_0_2_centos conf]#
启动mysql
docker start mysql
查看mysql容器
[root@VM_0_2_centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9fe468f1d89 mysql:5.7 "docker-entrypoint.s…" 51 minutes ago Up 7 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
[root@VM_0_2_centos /]#
进入mysql容器内部
docker exec -ti d9fe468f1d89 /bin/bash
登陆mysql
mysql -u root -p
修改root 可以通过任何客户端连接
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;
使用客户端测试
说明
本文只做学习参考,如有任何不准确的地方欢迎指正。
我的邮箱:
lulongji2011@163.com