翼度科技»论坛 编程开发 mysql 查看内容

docker安装mysql8.0.20并远程连接

9

主题

9

帖子

27

积分

新手上路

Rank: 1

积分
27
前言

今天docker安装mysql8.0.20捯饬了半天,主要是挂载问题和连接问题,索性记录一下。网上很多千篇一律,还有很多就是过时了,那还是我自己上场吧。大家看的时候,请睁大眼睛,按步骤来。
Docker安装MySQL8.0.20

此处默认你已经搭建好了docker环境
第一步 拉镜像

docker pull mysql:8.0.20
第二步 启动

docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456  -d mysql:8.0.20
第三步 复制

将容器内mysql的数据配置复制到本机,后面那个路径就是你想要映射的文件地址
docker cp  mysql:/etc/mysql /root/mysql8.0.20
建议授权一下文件夹 防止权限问题异常, 进入到root目录
chmod 777 mysql8.0.20
第四步 删除旧容器

docker stop mysql && docker rm mysql
第五步 重新按以下命令启动, 建议自己保存下来

如果不熟悉,建议不要改动。映射端口以及容器名称可自行更改
docker run \ -p 3306:3306 \ --name mysql8 \ --privileged=true \ --restart unless-stopped \ -v /root/mysql8.0.20/mysql:/etc/mysql \ -v /root/mysql8.0.20/logs:/logs \ -v /root/mysql8.0.20/data:/var/lib/mysql \ -v /root/mysql8.0.20/mysql/mysql-files:/var/lib/mysql-files \ -v /etc/localtime:/etc/localtime \ -e MYSQL_ROOT_PASSWORD=123456 \ -d mysql:8.0.20
如果缺少 -v /root/mysql8.0.20/mysql/mysql-files:/var/lib/mysql-files 这个会报异常
到这里基本完成一半。
登录并远程连接

进入容器并登录,你会发现登录不了
进入mysql8容器
docker exec -it mysql8 /bin/bash
登录
mysql -uroot -p ,输入密码 发现登录不了. 输入 exit 先退出容器
修改配置文件

进入 /root/mysql8.0.20 文件, 编辑 my.cnf, 在[mysqld]增加一行 skip_grant_tables 此时mysql是无密码状态
重启容器

docker restart mysql8
再次进入容器

参考上述登录,再次输入 mysql -uroot -p 连按两次回车可登录成功 显示如下:
root@15006e4d70b3:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
查看表

mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| component                 |
| db                        |
| default_roles             |
| engine_cost               |
| func                      |
| general_log               |
| global_grants             |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| password_history          |
| plugin                    |
| procs_priv                |
| proxies_priv              |
| role_edges                |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
33 rows in set (0.00 sec)
查看用户表

mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.01 sec)
因为 caching_sha2_password  ,所以使用密码登录是不行的,需要修改
修改plugin

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
这个时候 root还是不能远程登录,会报错,提示找不到ip。此处的密码,最好设置复杂一点,确保一次性通过.
修改远程连接

mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
确认是否更改

mysql> select host,user,plugin from 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 |
+-----------+------------------+-----------------------+
可以看到已经更改成功了,别高兴太早,这时候还是连不上,需要刷新以下权限!
刷新权限

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
这样就搞定了
使用navicat连接一下试试

连接成功!
善后

还记得之前增加的那个 skip_grant_tables  吗,要把它注释掉。然后重启就算完结了。
如果还是连不上,确保你的服务器端口是正常放行的,可以去云服务器安全组查看一下。

来源:https://www.cnblogs.com/amazingjava/p/18145314
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具