---------------------------5.7-------------------------------
docker中连接mysql容器时,需要ip地址的话 docker inspect 容器名 查看
1 yin5th@yin5th:~/code/docker/compose-golang$ docker inspect compose-golang_mysql_1 | grep IPAddress 2 "SecondaryIPAddresses": null, 3 "IPAddress": "", 4 "IPAddress": "172.22.0.2", 5 yin5th@yin5th:~/code/docker/compose-golang$
开启远程访问权限。
a.docker exec -it mysql容器名 /bin/bash
b.mysql -uroot -p
c.进入mysql数据库(use mysql;)执行下列命令: (赋予root用户所有ip连接数据库的权限)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; flush privileges;
-----------------------myql 8.0+ -----------------------------------------------
如果mysql版本是8.0以上在授权的语句中需要去掉 IDENTIFIED BY ‘password’;否则会报语法错误
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
mysql 8.0中,第一次创建用户时,可能会报错
ERROR 1146 (42S02): Table 'mysql.role_edges' doesn't exist
需要退出mysql执行:
sudo mysql_upgrade -uroot -p