Docker 安装 mysql8.0
1. 获取mysql:拉去mysql镜像
docker pull mysql:8.0
2. 启动mysql
# --name指定容器名字 -v目录挂载 -p指定端口映射 -e设置mysql参数 -d后台运行
# MYSQL_ROOT_PASSWORD=root root自定义密码
docker run --name mysql -v /usr/local/mysql/data:/var/lib/mysql -v /usr/local/mysql:/etc/mysql/conf.d -v /usr/local/mysql/log:/var/log/mysql -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -d mysql:8.0
3. 进入mysql容器
# 设置mysql开机自启动(可选)
docker update mysql --restart=always
docker exec -it 容器名称|容器id bin/bash
docker exec -it mysql bin/bash
4. 登录mysql,并打开远程访问授权
//1.登录mysql
mysql -uroot -p
//2.进行授权
GRANT ALL ON *.* TO 'root'@'%';
//3.刷新权限
flush privileges;
//4.加密规则
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
//5.更新root密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
//6.刷新权限
flush privileges;
5.完整代码
[root@centos7964 ~]# docker run --name mysql -v /usr/local/mysql/data:/var/lib/SSWORD=root -p 3306:3306 -d mysql:8.0
Unable to find image 'mysql:8.0' locally
8.0: Pulling from library/mysql
c1ad9731b2c7: Pull complete
54f6eb0ee84d: Pull complete
cffcf8691bc5: Pull complete
89a783b5ac8a: Pull complete
6a8393c7be5f: Pull complete
af768d0b181e: Pull complete
810d6aaaf54a: Pull complete
2e014a8ae4c9: Pull complete
a821425a3341: Pull complete
3a10c2652132: Pull complete
4419638feac4: Pull complete
681aeed97dfe: Pull complete
Digest: sha256:548da4c67fd8a71908f17c308b8ddb098acf5191d3d7694e56801c6a8b2072cc
Status: Downloaded newer image for mysql:8.0
0481193998bc22766f3dfee505e4bcf7ab722a46fae4a9dbdb21ed3a73404102
[root@centos7964 ~]# docker update mysql --restart=always
mysql
[root@centos7964 ~]# docker exec -it mysql bin/bash
root@0481193998bc:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.29 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> GRANT ALL ON *.* TO 'root'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
. 使用远程登录进行测试