共计 1750 个字符,预计需要花费 5 分钟才能阅读完成。
使用 navicat 连接
1. 下载 Mysql 的 Docker 镜像:
$ docker search mysql (搜索 mysql 镜像)
$ docker pull mysql(下载 mysql 镜像,默认最新版本)
2. 运行镜像:
$ docker run -it --rm --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 -d mysql
设置 root 账号初始密码(123456),映射本地宿主机端口 3307 到 Docker 端口 3306。测试过程没有挂载本地数据盘
3. 查看已运行的容器:
[root@wellhey ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46bcd0adc1c3 mysql:latest "docker-entrypoint.s…" About an hour ago Up About an hour 33060/tcp, 0.0.0.0:3307->3306/tcp mysql
2afd2a2165b4 portainer/portainer "/portainer" 10 hours ago Up 10 hours 0.0.0.0:9000->9000/tcp quizzical_neumann
4. 进入 mysql 容器:
docker exec -it mysql bash
root@a42f31094df5:/#
5. 在容器内登陆 Mysql:
[root@wellhey ~]# docker exec -it mysql mysql -uroot -p
Enter password:
'Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective owners.
mysql>
6. 查看用户信息并修改 root 权限
mysql> select host,user,plugin from mysql.user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| % | root | mysql_native_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | caching_sha2_password |
+-----------+------------------+-----------------------+
备注:host 为 % 表示不限制 ip localhost 表示本机使用 plugin 非 mysql_native_password 则需要修改密码。
navicat 链接错误,我们继续往下看。
mysql> ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
Query OK, 0 rows affected (0.01 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
7. 连接数据库的
8. 其他错误
此方法修改 root 权限还可以解决 MySQL 登录时出现:Access denied for user ‘root’@’xxxxxx’ (using password: YES)
正文完