配置容器

下载mysql容器

  1. $ docker pull mysql

将本地33306端口映射到容器3306端口

$ docker run -di --name docker_mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

进入容器

$ docker exec -it docker_mysql /bin/bash

/opt/app/mysql/my.cnf

-v /opt/app/mysql/my.cnf:/etc/mysql/my.cnf

设置远程登录

登录mysql,密码就是我们刚刚设置的 MYSQL_ROOT_PASSWORD=123456

root@8b7eb4eb9407:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.44 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

root 远程登录权限

mysql> GRANT ALL ON *.* TO 'root'@'%';
Query OK, 0 rows affected (0.23 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

退出mysql,退出容器

mysql> exit
Bye
root@8b7eb4eb9407:/# exit
exit

Navicat 测试连接

image.png

Tip:
在docker中运行mysql可能会内存占用过高,mysql会动不动就挂掉,可以通过修改mysql配置文件的方法来改善这种情况:https://www.jianshu.com/p/5a344c258d5f

PS

Authentication plugin ‘caching_sha2_password’ cannot be loaded

#修改加密规则 
$ ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; 

#更新一下用户密码 
$ ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

#刷新权限 
$ FLUSH PRIVILEGES;
vim /etc/mysql/mysql.conf.d/mysqld.cnf

docker logs -f 8ffd1ed18e8f

ps auxw --sort=-rss | head