mysql8.0其他机器访问_MySQL8.0允许外部访问

本文介绍解决MySQL 8.0外部访问问题的方法,包括登录MySQL、更新域属性、刷新权限、执行授权语句等。还提及可能出现的连接报错,如客户端不支持加密协议,给出修改加密模式的方案。若仍无法连接,可能是防火墙问题,并给出防火墙相关操作命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继上一篇安装mysql 后外部访问mysql 时会有日下错误提示

下面就来解决一下

1,登进MySQL,使用如下命令

mysql -uroot -p

#输入密码

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connectionid is 12Server 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 clearthe current input statement.

mysql>

2,输入以下语句,进入mysql库:

use mysql;

3,更新域属性,'%'表示允许外部访问:

mysql> update user set host='%' where user ='root';

Query OK,1 row affected (0.03sec)

Rows matched:1 Changed: 1 Warnings: 0

4,执行以上语句之后再执行:

FLUSH PRIVILEGES; #刷新权限

5,再执行授权语句:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION;

Query OK,0 rows affected (0.02 sec)

然后外部就可以通过账户密码访问了。

6,其它说明:

FLUSH PRIVILEGES; 命令本质上的作用是:

将当前user和privilige表中的用户信息/权限设置从mysql库(MySQL数据库的内置库)中提取到内存里。

MySQL用户数据和权限有修改后,希望在"不重启MySQL服务"的情况下直接生效,那么就需要执行这个命令。

通常是在修改ROOT帐号的设置后,怕重启后无法再登录进来,那么直接flush之后就可以看权限设置是否生效。

而不必冒太大风险。

三、可能存在的其它问题:

执行完之后,再用Navicat连接mysql,报错如下:

Client does not support authentication protocol requested by server;

报错原因:

mysql8.0 引入了新特性 caching_sha2_password;这种密码加密方式Navicat 12以下客户端不支持;

Navicat 12以下客户端支持的是mysql_native_password 这种加密方式;

解决方案:

1,用如下语句查看MySQL当前加密方式

mysql>use mysql;

Reading table informationforcompletion of table and column names

You can turn off this feature to get a quicker startup with-A

Database changed

mysql> selecthost,user,plugin from user;+-----------+------------------+-----------------------+

| host | user | plugin |

+-----------+------------------+-----------------------+

| % | root | caching_sha2_password |

| localhost | mysql.infoschema | caching_sha2_password |

| localhost | mysql.session | caching_sha2_password |

| localhost | mysql.sys | caching_sha2_password |

+-----------+------------------+-----------------------+

4 rows in set (0.00 sec)

看第一行,root加密方式为caching_sha2_password。

2,使用命令将他修改成mysql_native_password加密模式:

update user set plugin='mysql_native_password' where user='root';

再次连接的时候,就成功了。

四、如果还连接不上

通过以上操作后,依然无法连接上,问题可能出在了防火墙上。

1,MySQL部署在实体服务器上解决方案如下:

a.开放MySQL的端口号,默认端口号是3306。

参照防火墙命令7、8

b.直接关闭防火墙(慎重操作,不建议。)

参照防火墙知识 2、3

五、防火墙相关知识

Centos7默认安装了firewalld,如果没有安装的话,可以使用

yum install firewalld firewalld-config

进行安装。

1:查看防火状态

systemctl status firewalld

service iptables status

2:暂时关闭防火墙

systemctl stop firewalld

service iptables stop

3:永久关闭防火墙

systemctl disable firewalld

chkconfig iptables off

4:打开防火墙

systemctl enable firewalld

service iptables restart

5、将接口添加到区域(默认接口都在public)

firewall-cmd --zone=public --add-interface=eth0(永久生效再加上 --permanent 然后reload防火墙)

6、查看指定区域所有打开的端口

firewall-cmd --zone=public --list-ports

7、在指定区域打开端口(记得重启防火墙)

firewall-cmd --zone=public --add-port=8080/tcp(永久生效再加上 --permanent)

说明:

–zone 作用域

–add-port=8080/tcp 添加端口,格式为:端口/通讯协议

–permanent #永久生效,没有此参数重启后失效

8、重启防火墙

firewall-cmd --reload

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值