mysql配置中采用mysql skip-name-resolver提高性能

本文介绍了在Linux下安装MySQL时,为何建议修改my.cnf文件加入`skip-name-resolve`以避免DNS解析导致的连接延迟。通过理解MySQL的DNS查找过程,解释了该设置如何影响权限管理和性能优化,特别适用于开发机器无法连接外网的情况。

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

原地址:https://www.cnblogs.com/diruodaichang/p/11245213.html

在linux下安装mysql时,安装好后,还建议修改MySQL的登录设置:
# vi /etc/my.cnf
在[mysqld]的段中加上一句:skip-name-resolve

例如:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-name-resolve

查了一下资料,了解到mysql skip-name-resolve 的作用了。具体如下:

mysql skip-name-resolve 的解释
mysql连接很慢,登陆到服务器上查看服务器日志都是正常的,无可疑记录,登陆到mysql服务器上,查看下进程,发现有很多这样的连接:
218 | unauthenticated user | 192.168.10.6:44500 | NULL | Connect | NULL | login | NULL
219 | unauthenticated user | 192.168.10.6:44501 | NULL | Connect | NULL | login | NULL

原因是由于mysql对连接的客户端进行DNS反向解析。
有2种解决办法:
1,把client的ip写在mysql服务器的/etc/hosts文件里,随便给个名字就可以了。
2,在 my.cnf 中加入 –skip-name-resolve 。
对于第一种方法比较笨,也不实用,那么 skip-name-resolve 选项可以禁用dns解析,但是,这样不能在mysql的授权表中使用主机名了,只能使用IP。
我理解mysql是这样来处理客户端解析过程的,
1,当mysql的client连过来的时候,服务器会主动去查client的域名。
2,首先查找 /etc/hosts 文件,搜索域名和IP的对应关系。
3,如果hosts文件没有,则查找DNS设置,如果没有设置DNS服务器,会立刻返回失败,就相当于mysql设置了skip-name-resolve参数,如果设置了DNS服务器,就进行反向解析,直到timeout。

所谓反向解析是这样的:
mysql接收到连接请求后,获得的是客户端的ip,为了更好的匹配mysql.user里的权限记录(某些是用hostname定义的)。
如果mysql服务器设置了dns服务器,并且客户端ip在dns上并没有相应的hostname,那么这个过程很慢,导致连接等待。

添加skip-name-resolve以后就跳过着一个过程了。

官方的解释

How MySQL
uses DNS When a new thread connects to mysqld, mysqld will
spawn a new thread to handle the request. This thread will first check
if the hostname is in the hostname cache. If not the thread will call
gethostbyaddr_r() and gethostbyname_r() to resolve the hostname. If
the operating system doesn’t support the above thread-safe calls, the
thread will lock a mutex and call gethostbyaddr() and gethostbyname()
instead. Note that in this case no other thread can resolve other
hostnames that is not in the hostname cache until the first thread is
ready. You can disable DNS host lookup by starting mysqld with
–skip-name-resolve. In this case you can however only use IP names in
the MySQL privilege tables. If you have a very slow DNS and many
hosts, you can get more performance by either disabling DNS lookop
with –skip-name-resolve or by increasing the HOST_CACHE_SIZE define
(default: 128) and recompile mysqld. You can disable the hostname
cache with –skip-host-cache. You can clear the hostname cache with
FLUSH HOSTS or mysqladmin flush-hosts. If you don’t want to allow
connections over TCP/IP, you can do this by starting mysqld with
–skip-networking.

根据文档说明,如果你的mysql主机查询DNS很慢或是有很多客户端主机时会导致连接很慢,由于我们的开发机器是不能够连接外网的,所以DNS解析是不可能完成的,从而也就明白了为什么连接那么慢了。同时,请注意在增加该配置参数后,mysql的授权表中的host字段就不能够使用域名而只能够使用 ip地址了,因为这是禁止了域名解析的结果。

### 如何在 Eclipse IDE 中配置和使用 SSM 框架 #### 创建 Maven 项目并添加依赖 为了开始构建基于 SSM 的应用程序,首先需要创建一个新的 Maven 项目。这可以通过 Eclipse 内置的支持完成。确保选择了 Maven Project 并勾选 "Create a simple project (skip archetype selection)" 来简化流程。 接着,在 `pom.xml` 文件中加入必需的依赖库以支持 Spring, Spring MVC 和 MyBatis 功能[^1]: ```xml <dependencies> <!-- Spring Framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- Other dependencies... --> </dependencies> ``` #### 配置 Spring 和 MyBatis 一旦完成了基础设置,则要继续配置 Spring 容器以便能够管理 Bean 生命周期和服务注入等功能。同样重要的是定义数据源、事务管理和映射接口等与持久层有关的内容。 对于 MyBatis 而言,通常会在 XML 映射文件里指定 SQL 查询语句,并通过 Java 接口访问这些查询方法。此外还需要编写相应的 Mapper 类型别名注册表以及扫描包路径让应用知道去哪里寻找 Mappers[^2]。 #### 整合 SpringMVC 及其配置 为了让前端请求可以被正确路由至控制器处理程序,必须编辑 web 应用程序描述符 (`web.xml`) 添加 DispatcherServlet 初始化参数指向 spring-mvc 上下文位置。另外还需建立视图解析器用于渲染 JSP 页面或其他模板引擎输出的结果[^3]: ```xml <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- View Resolver Configuration --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> ``` #### 数据库连接和其他准备工作 最后一步涉及到了实际的数据存储部分—即 MySQL 或其他关系型数据库管理系统(RDBMS)的选择。利用 Navicat 等工具预先准备好所需的模式(schema),并将 JDBC URL、用户名密码等相关信息填入项目的 `jdbc.properties` 文件内[^4]。 至此,整个 SSM 开发环境已经在 Eclipse 下搭建完毕,开发者可以根据具体业务需求进一步完善各个组件之间的交互逻辑了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值