当前位置:网站首页>解决在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即可成功~
边栏推荐
- Redis distributed lock implementation redison 15 questions
- Retention policy of RMAN backup
- Control the operation of the test module through the panel in canoe (Advanced)
- Tianmu MVC audit I
- MySQL实战优化高手08 生产经验:在数据库的压测过程中,如何360度无死角观察机器性能?
- [NLP] bert4vec: a sentence vector generation tool based on pre training
- Day 5 of MySQL learning
- 单片机实现模块化编程:思维+实例+系统教程(实用程度令人发指)
- 再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
- Control the operation of the test module through the panel in canoe (primary)
猜你喜欢

What should the redis cluster solution do? What are the plans?

Listen to my advice and learn according to this embedded curriculum content and curriculum system

大学C语言入门到底怎么学才可以走捷径

单片机实现模块化编程:思维+实例+系统教程(实用程度令人发指)

Several silly built-in functions about relative path / absolute path operation in CAPL script

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

jar运行报错no main manifest attribute

Jar runs with error no main manifest attribute

15 医疗挂号系统_【预约挂号】

CAPL 脚本打印函数 write ,writeEx ,writeLineEx ,writeToLog ,writeToLogEx ,writeDbgLevel 你真的分的清楚什么情况下用哪个吗?
随机推荐
Safety notes
C#/. Net phase VI 01C Foundation_ 01: running environment, process of creating new C program, strict case sensitivity, meaning of class library
How can I take a shortcut to learn C language in college
在CANoe中通過Panel面板控制Test Module 運行(初級)
Competition vscode Configuration Guide
简单解决phpjm加密问题 免费phpjm解密工具
软件测试工程师必备之软技能:结构化思维
CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
AI的路线和资源
CAPL script pair High level operation of INI configuration file
学习单片机对社会的帮助是很大的
Compress decompress
Why is 51+ assembly in college SCM class? Why not come directly to STM32
Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience
Preliminary introduction to C miscellaneous lecture document
寶塔的安裝和flask項目部署
安装OpenCV时遇到的几种错误
嵌入式开发比单片机要难很多?谈谈单片机和嵌入式开发设计经历
Can I learn PLC at the age of 33
[NLP] bert4vec: a sentence vector generation tool based on pre training