1、拉取官方mysql5.7镜像
docker pull mysql:5.7
root@VM-12-5-ubuntu:/# docker pull mysql:5.7
5.7: Pulling from library/mysql
ffbb094f4f9e: Pull complete
df186527fc46: Pull complete
fa362a6aa7bd: Pull complete
5af7cb1a200e: Pull complete
949da226cc6d: Pull complete
bce007079ee9: Pull complete
eab9f076e5a3: Pull complete
c7b24c3f27af: Pull complete
6fc26ff6705a: Pull complete
bec5cdb5e7f7: Pull complete
6c1cb25f7525: Pull complete
Digest: sha256:d1cc87a3bd5dc07defc837bc9084f748a130606ff41923f46dec1986e0dc828d
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
root@VM-12-5-ubuntu:/#
3、查看镜像库
docker images
root@VM-12-5-ubuntu:/# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 738e7101490b 8 days ago 448MB
root@VM-12-5-ubuntu:/#
1、在本地创建mysql的映射目录
mkdir -p /root/mysql/data /root/mysql/logs /root/mysql/conf
2、在/root/mysql/conf中创建 *.cnf 文件(叫什么都行)
touch my.cnf
3、创建容器,将数据,日志,配置文件映射到本机
docker run -p 3306:3306 --name mysql -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/logs:/logs -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
-d: 后台运行容器
-p 将容器的端口映射到本机的端口
-v 将主机目录挂载到容器的目录
-e 设置参数
4、启动mysql容器(一般是停止过后)
docker start mysql
5、查看/root/mysql/data目录是否有数据文件
root@VM-12-5-ubuntu:~/mysql/conf# ls /root/mysql/data/
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem
root@VM-12-5-ubuntu:~/mysql/conf#
6、服务器注意要开放端口3306
7、mysql允许远程连接(不开启可能会存在连不上的问题)
docker exec -it mysql /bin/bash 或者 docker exec -it 容器ID /bin/bash ## 进入容器
mysql -uroot -p ## 登录myusql,会提示输入密码,输入之前容器初始化的密码即可
## 登录数据库 **修改使用admin登录配置**
mysql> grant all on *.* to admin@'%' identified by 'xxxxxx' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
## 登录数据库 **修改使用root登录配置:root用户记得是 'root'**
mysql> grant all on *.* to 'root'@'%' identified by 'root' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)