菜单

波波
发布于 2019-12-17 / 0 阅读
0

【Docker】Docker中安装的MySQL数据库支持远程连接

Docker中安装的MySQL 同事设置的不支持远程连接。 他请假了,然后电话也没打通,只有自己在服务器上设置下了。 命令行操作说实话给表里加索引啥的,操作不太方便。

我们开始吧!

1. 登入到服务器,执行进入docker命令行指令

[root@izbp13hwvp8k9hwh4rrjvpz tc-cloud]# docker exec -it 4d51d290be28 bash
root@4d51d290be28:/# 

2. 执行mysql登陆命令

mysql -u root -p

3.MySQL内执行

root@4d51d290be28:/# mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 181
Server version: 8.0.18 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> 
mysql> 
mysql> 
mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| cloud_auth         |
| cloud_central      |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host      | user             | plugin                | authentication_string                                                  |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session    | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys        | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root             | mysql_native_password | *32F5D03C7C7E949141AC63A9934D865AA09B4795                              |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)

mysql> 
mysql> ALTER USER 'root'@'172.x.x.x' IDENTIFIED WITH mysql_native_password BY 'gyb2LigLajdFFGZRikf';
ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'172.17.0.2'
mysql> ALTER USER 'root'@'172.x.x.x' IDENTIFIED WITH mysql_native_password BY 'gyb2LigLajdFFGZRikf';
ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'172.17.0.2'
mysql> 
mysql> 
mysql> 
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>  select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)

mysql> 
mysql> 
mysql> 
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'gyb2LigLajdFFGZRikf';
Query OK, 0 rows affected (0.01 sec)

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

mysql> 
mysql> 
mysql> 
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host      | user             | plugin                | authentication_string                                                  |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session    | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys        | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root             | mysql_native_password | *32F5D03C7C7E949141AC63A9934D865AA09B4795                              |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)

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

mysql> 
mysql> 
mysql> 
mysql> 
mysql> select host from user;
+-----------+
| host      |
+-----------+
| localhost |
| localhost |
| localhost |
| localhost |
+-----------+
4 rows in set (0.00 sec)

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

mysql> 
mysql> select host from user;
+-----------+
| host      |
+-----------+
| %         |
| localhost |
| localhost |
| localhost |
+-----------+
4 rows in set (0.00 sec)

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


说到底就是让Mysql的user用户表中的root账户或者特定账户的host改为'%'.然后刷新权限。当然,你的服务器的防火墙也要打开的。

操作完后就原路,关防火墙,把Mysql的user用户表中的root账户或者特定账户的host改为'localhost'.然后刷新权限.

是不是很easy.