开发某应用系统连接公司的测试服务器的mysql数据库连接打开的很慢,但是连接本地的mysql数据库很快,刚开始认为可能是网络连接问题导致的,在进行 ping和route后发现网络通信都是正常的,而且在mysql机器上进行本地连接发现是很快的,所以网络问题基本上被排除了,所以想看看是不是mysql的配置问题。在查询mysql相关文档和网络搜索后,发现了一个配置似乎可以解决这样的问题,就是在mysql的配置文件中增加如下配置参数:

[mysqld]
skip-name-resolve

在linux下配置文件是/etc/my.cnf,在windows下配置文件是mysql安装目录下的my.ini文件。注意该配置是加在 [mysqld]下面,在更改配置并保存后,然后重启mysql并远程连接测试,一切恢复如初。该参数的官方解释信息如下:

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地址了,因为这是禁止了域名解析的结果。

解决远程连接mysql很慢的方法的更多相关文章

  1. 解决远程连接mysql很慢的方法(网络正常)

    最近用mysql命令行或者JDBC远程连接mysql速度很慢,而且远大于ping时间.上网搜了一下,解决方案如下: 在/etc/mysql/my.cnf文件的[mysqld]部分加入:skip-nam ...

  2. 解决远程连接mysql很慢的方法(mysql_connect 打开连接慢)

    http://www.jb51.net/article/27616.htm   有次同事提出开发使用的mysql数据库连接很慢,因为我们的mysql开发数据库是单独一台机器部署的,所以认为可能是网络连 ...

  3. mysql之 远程连接 mysql 很慢,本地连接 mysql 很快 (skip-name-resolve)

    症状:,远程连接 mysql 很慢,但是 本地连接 mysql 很快, ping 和 route 网络通信都是正常的. 解决:mysql 的配置文件中增加如下配置参数:[mysqld]skip-nam ...

  4. 如何解决远程连接mysql出现Can’t connect to MySQL server on (111 “Connection refused”)的问题

    如何解决远程连接mysql出现Can’t connect to MySQL server on (111 “Connection refused”)的问题 开放Mysql的远程连接 在服务器上登录my ...

  5. 解决远程连接mysql错误1130

    Mysql远程连接Mysql服务器的数据库,错误代码是1130,ERROR 1130: Host xxx.xxx.xxx.xxx  is not allowed to connect to this ...

  6. 解决远程连接mysql错误1130代码的方法

    命令行登陆 mysql -uroot -p 输入密码后登陆 use mysql;   select host,user from user ;     grant allon *.*to root i ...

  7. 远程连接mysql速度慢的解决方法:skip-name-resolve取消DNS的反向解析

    PHP远程连接MYSQL速度慢,有时远程连接到MYSQL用时4-20秒不等,本地连接MYSQL正常,出现这种问题的主要原因是,默认安装的 MYSQL开启了DNS的反向解析,在MY.INI(WINDOW ...

  8. 远程连接MySQL,防火墙阻止访问,解决方法

    远程连接MySQL,防火墙阻止访问,解决方法   xp/2003添加防火墙例外端口 打开防火墙,选择例外选项卡,添加端口 名称:mysqlport 端口号:3306 选中TCP win7添加防火墙例外 ...

  9. 远程连接MySQL错误“plugin caching_sha2_password could not be loaded”的解决办法

    远程连接MySQL错误"plugin caching_sha2_password could not be loaded"的解决办法 问题描述: 今天在阿里云租了一个服务器,当我用 ...

随机推荐

  1. LOJ.6053.简单的函数(Min_25筛)

    题目链接 Min_25筛见这里: https://www.cnblogs.com/cjyyb/p/9185093.html https://www.cnblogs.com/zhoushuyu/p/91 ...

  2. JAVA实现Word(doc)文件读写

    1.pom.xml依赖 <dependencies> <dependency> <groupId>org.apache.poi</groupId> &l ...

  3. ES6快速入门(三)类与模块

    类与模块 一.类 一)类的声明 class Person { constructor(name) { this.name = name; } sayName() { console.log(this. ...

  4. [CC-COUPLES]Couples sit next to each other

    [CC-COUPLES]Couples sit next to each other 题目大意: 有\(n(n\le5\times10^5)\)对小伙伴共\(2n\)个人坐成一圈.刚开始编号为\(i\ ...

  5. NOIP考纲总结+NOIP考前经验谈

    首先来一张图,很直观(截止到2012年数据) 下面是收集的一些,我改了一下 红色加粗表示特别重要,必须掌握 绿色加粗表示最好掌握,可能性不是很大,但是某些可以提高程序效率 高精度 a.加法 b.减法 ...

  6. [jzoj]3760.【BJOI2014】Euler

    Link https://jzoj.net/senior/#main/show/3760 Description 欧拉函数  φ(n)  定义为不超过正整数 n 并且与 n 互素的整数的数目. 可以证 ...

  7. Jsp俩大内置对象学习

    https://www.cnblogs.com/smyhvae/p/4065790.html post与get的区别 最直观的区别就是GET把参数包含在URL中,POST通过request body传 ...

  8. Android requestcode resultcode的作用

    requestcode 一个页面的不同事件,激发不同的函数,startActivityForResult中传入不同的请求码的值以调用下一个界面,在被调用界面结束返回第一个界面时,请求码会自动返回(自动 ...

  9. firefox镜像 和geckodriver驱动大全

    最近学习Selenium,下载资源很难,还好找到了一个网站,转载的https://blog.csdn.net/cyjs1988/article/details/73039423,收下了,以便以后学习使 ...

  10. JQ02

    JQ02 1.css操作 .css(属性名,属性值)://要有双引号 修改单个样式,若要修改多个,需采用以下方式: 以对象为参数 它还可以获取样式: .css("属性名") 若元素 ...