1. 下载Mysql的Docker镜像:
docker pull mysql:8.0.18
2. 运行镜像,设置root账号初始密码(123456),映射本地宿主机端口3306到Docker端口3306。
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /usr/local/mysql/data:/var/lib/mysql:rw -v /usr/local/mysql/mysql-files:/var/lib/mysql-files:rw -v /usr/local/mysql/log:/var/log/mysql:rw -v /usr/local/mysql/config:/etc/mysql:rw -v /etc/localtime:/etc/localtime:ro --restart=always -d mysql:8.0.18
3. 查看已运行的容器:
docker ps -a
4. 进入mysql容器:
docker exec -it mysql bash
5. 在容器内登陆Mysql:
mysql -uroot -p123456
6. 查看用户信息
select host,user,plugin,authentication_string from mysql.user;
host为 % 表示不限制ip localhost表示本机使用 plugin如果不是mysql_native_password 则需要修改
ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' FLUSH PRIVILEGES;
7.退出
mysql> exit;
ctrl+d 退出容器且关闭, docker ps 查看无
ctrl+p+q 退出容器但不关闭, docker ps 查看有