当前位置:网站首页>解决在window中远程连接Linux下的MySQL
解决在window中远程连接Linux下的MySQL
2022-07-06 09:10:00 【Eric-x】
前言
在实际开发中,我们连接数据库肯定是远程连接的,那这是如何设置的呢,让我们来探究一下。
准备条件:一台装有MySQL数据库的虚拟机,一台装有MySQL数据库的window
1、问题
在用SQLyog或Navicat中配置远程连接Mysql数据库时遇到如下报错信息,这是由于Mysql配置了不支持远程连接引起的。
2、确认网络
在window上使用ping ip地址 保证网络畅通
例如:ping 192.168.1.150
开启window中的Telnet客户端
打开控制面板,选择小图标展示,点击程序和功能,再点击启用或关闭window功能
将Telnet客户端勾选上
3、关闭防火墙或者开放端口
想要连接Linux下的MySQL,需要关闭防火墙或者开放端口,选择其中一种方式即可
方式1:关闭防火墙
CentOS6指令:service iptables stop
CentOS7指定:systemctl stop firewalld.service
设置开机禁用防火墙:systemctl disable firewalld.service
方式2:开放端口
//查看开放的端口号
firewall-cmd --list-all
//设置开放的端口号
firewall-cmd --add-service=http --permanent
firewall-cmd --add-port=3306/tcp --permanent
//重启防火墙
firewall-cmd --reload
4、修改Linux下的配置
1、打开虚拟机,连接Linux下的MySQL,使用如下sql
use mysql;
select Host,User from user;
可以看到root用户的当前主机配置信息为localhost。就代表着使用root用户那么只能本地连接数据库
修改Host为通配符%
Host列指定了允许用户登录所使用的IP,比如user=root Host=192.168.1.1。这里的意思就是说root用户只能通过192.168.1.1的客户端去访问。 user=root Host=localhost,表示只能通过本机客户端去访问。而 %是个 通配符 ,如果Host=192.168.1.%,那么就表示只要是IP地址前缀为“192.168.1.”的客户端都可以连接。如果 Host=% ,表示所有IP都有连接权限。
注意:在生产环境下不能为了省事将host设置为%,这样做会存在安全问题,具体的设置可以根据生产环境的IP进行设置
update user set host = '%' where user ='root';
Host设置了“%”后便可以允许远程访问。
Host修改完成后记得执行flush privileges使配置立即生效:
flush privileges;
5、测试
- 如果是 MySQL5.7 版本,接下来就可以使用SQLyog或者Navicat成功连接至MySQL了。
- 如果是 MySQL8 版本,连接时还会出现如下问题:

配置新连接报错:错误号码 2058,分析是 mysql 密码加密方法变了
解决方法:Linux下 mysql -u root -p 登录你的 mysql 数据库,然后 执行这条SQL:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'abc123';
然后在重新配置SQLyog的连接,则可连接成功了,搞定收工。
总结
其实过程还是很简单的:
- 首先检查本机是否能ping通虚拟机IP地址
- 开启window中的Telnet客户端服务
- 关闭Linux的防火墙(或者开放3306端口)
- 修改Linux下MySQL的远程连接配置(记得立即刷新一下)
- 最后如果是MySQL8.0再使用如上SQL设置MySQL即可成功~
边栏推荐
- 通过bat脚本配置系统环境变量
- 单片机如何从上电复位执行到main函数?
- Compress decompress
- Contest3145 - the 37th game of 2021 freshman individual training match_ B: Password
- C杂讲 双向循环链表
- If a university wants to choose to study automation, what books can it read in advance?
- Control the operation of the test module through the panel in canoe (Advanced)
- 14 医疗挂号系统_【阿里云OSS、用户认证与就诊人】
- 在CANoe中通过Panel面板控制Test Module 运行(初级)
- 如何让shell脚本变成可执行文件
猜你喜欢

Target detection -- yolov2 paper intensive reading

Preliminary introduction to C miscellaneous lecture document

在CANoe中通过Panel面板控制Test Module 运行(初级)

Programmation défensive en langage C dans le développement intégré

51单片机进修的一些感悟

History of object recognition

Control the operation of the test module through the panel in canoe (primary)

Nc17 longest palindrome substring

Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions

17 医疗挂号系统_【微信支付】
随机推荐
在CANoe中通過Panel面板控制Test Module 運行(初級)
Can I learn PLC at the age of 33
竞赛vscode配置指南
Routes and resources of AI
Inject common SQL statement collation
vscode 常用的指令
Tianmu MVC audit II
Installation de la pagode et déploiement du projet flask
如何让shell脚本变成可执行文件
NLP routes and resources
安装OpenCV时遇到的几种错误
Listen to my advice and learn according to this embedded curriculum content and curriculum system
Installation of pagoda and deployment of flask project
Selection of software load balancing and hardware load balancing
CANoe仿真功能之自动化序列(Automation Sequences )
CANoe CAPL文件操作目录合集
[one click] it only takes 30s to build a blog with one click - QT graphical tool
Jar runs with error no main manifest attribute
A new understanding of RMAN retention policy recovery window
17 医疗挂号系统_【微信支付】