docker安装mysql5.7

一.安装MYSQL

查找MySQL镜像:

docker search mysql

下载MySQL镜像(:5.7 表示5.7版本)

docker pull mysql:5.7

运行MySQL容器:

docker run --name mysql -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=XiaoMeng666 mysql:5.7

二. MySQL远程访问

查看容器

docker ps -a

进入容器

docker exec -it <容器名或容器ID> bash

进入mysql

root@7e8060b03125:/# mysql -u root -p                # 登录mysql服务器
# 在这里输入mysql密码:123456
mysql> show databases;                              # 查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.09 sec)
mysql> select host, user,plugin,authentication_string from mysql.user;
+-----------+---------------+-----------------------+-------------------------------------------+
| host      | user          | plugin                | authentication_string                     |
+-----------+---------------+-----------------------+-------------------------------------------+
| localhost | mysql.session | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys     | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| %         | root          | mysql_native_password | *50208BAA1E29F372145D89CC187279F75FBEC2CC |
+-----------+---------------+-----------------------+-------------------------------------------+
3 rows in set (0.00 sec)
# 备注:host为 % 表示不限制ip
# localhost表示本机使用 
# plugin 非mysql_native_password则需要修改密码

也有可能需要

mysql> update mysql.user set host='%' where user='root';     # 更新 root的 远程登录为所有
Query OK, 1 row affected (0.10 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql> select host, user,plugin,authentication_string from mysql.user;
+-----------+---------------+-----------------------+-------------------------------------------+
| host      | user          | plugin                | authentication_string                     |
+-----------+---------------+-----------------------+-------------------------------------------+
| localhost | mysql.session | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys     | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| %         | root          | mysql_native_password | *50208BAA1E29F372145D89CC187279F75FBEC2CC |
+-----------+---------------+-----------------------+-------------------------------------------+
3 rows in set (0.00 sec)
# 备注:host为 % 表示不限制ip localhost表示本机使用 plugin非mysql_native_password 则需要修改密码
mysql> flush privileges;                    # 刷新权限
Query OK, 0 rows affected (0.03 sec)

但是还报错了,报错内容不一样了:
1045 Access denied for user 'root'@'192.168.31.43' (usingpassword:YES)
解决方法如下:

mysql> grant all privileges on *.* to root@'%' identified by '123456';    # 给用户授权
Query OK, 0 rows affected, 1 warning (0.11 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.04 sec)

刷新授权

flush privileges;

退出
exit退出mysql

exit

exit退出容器的bash

exit

参考:https://blog.csdn.net/weixin_39456915/article/details/105230544

你可能感兴趣的:(docker安装mysql5.7)