Home Database Mysql Tutorial 如何解决局域网内mysql数据库连接慢_MySQL

如何解决局域网内mysql数据库连接慢_MySQL

May 30, 2016 pm 05:10 PM

通过内网连另外一台机器的mysql服务, 确发现速度N慢! 等了大约几十秒才等到提示输入密码。  但是ping mysql所在服务器却很快!  想到很久之前有过类似的经验, telnet等一些服务在连接请求的时候,会做一些反向域名解析(如果反向解析不顺畅, 估计就会延迟很多,  给人的感觉是连接速度N慢!)
    在网上搜索了下解决办法, 发现有两种:

    一. 两边机器配置的resolve.conf不一样, 把resolve.conf配置成速度比较快的DNS就OK

    二. 把mysql配置中的DNS反向解析关掉, 也OK 

我用的是第一种办法, 修改resolve.conf配置后, 速度就上来了! 

附一: 摘自http://gcoder.blogbus.com/logs/31907502.html

    两台在同一网段的机器, 连接同一个数据库服务器, 一台的速度是另一台的100倍, 数据库主机是域名表示的. 怎么都想不明白?

    后来查看两台机器的 /etc/resolv.conf, 发现配置不一样, 改成速度快的那台机器的配置, 速度那上就上来了.

    域名解析也可以导致网络程序慢, 又一收获. 另外, mysql还有DNS反向解析的问题, 也可能导致速度慢, 可以在mysql的配置文件中, 把DNS反向解析关掉. 

    附二: 摘自

    当远程访问mysql时,mysql会解析域名,会导致访问速度很慢,加上下面这个配置可解决此问题

 # 禁止mysql做域名解析
  [mysqld]
  skip-name-resolve
Copy after login

PHP远程连接MYSQL速度慢,有时远程连接到MYSQL用时4-20秒不等,本地连接MYSQL正常,出现这种问题的主要原因是,默认安装的MYSQL开启了DNS的反向解析,在MY.INI(WINDOWS系统下)或MY.CNF(UNIX或LINUX系统下)文件的[mysqld]下加入skip-name-resolve这一句。

但是,这样会引起一个问题:连接mysql时,不能使用 localhost连接了,而是要使用IP地址的;如果是按localhost对用户赋权限的话,用户登录权限也要修改一下的。

连接mysql速度慢的解决方法.

2台服务器,一台跑iis+php,一台跑mysql,和以往一样配置好环境,测试页面一切OK

跑应用的时候发现php访问mysql速度很慢,这种情况在以前从未发现过,虽然2台服务器并非在同一网段中,但是ping数值基本上都在1,2ms之间,tcp连接应该不是问题关健,google以后找到答案,在my.ini文件的[mysqld]部分加入:skip-name-resolve,保存文件,重启mysql,一切OK啦,速度象飞一样了

新版本的mysql配置起来不象以前的那个傻瓜化了,这个问题折腾了我一上午的时间,晚上回来总算是解决了,嘿嘿,又学到一些东西。

Windows 2003下的MySQL 5服务器,本机连接到MySQL服务非常快,局域网内有两台Linux机器,有一台连接很快,另外一台输入密码后要等好几秒钟才能连上。

解决办法:

在MySQL服务器的配置中增加一个如下配置后速度飞快。

[mysqld]
  skip-name-resolve
Copy after login

附录:( 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.

PHP远程连接MYSQL速度慢,有时远程连接到MYSQL用时4-20秒不等,本地连接MYSQL正常,出现这种问题的主要原因是,默认安装的MYSQL开启了DNS的反向解析,在MY.INI(WINDOWS系统下)或MY.CNF(UNIX或LINUX系统下)文件的[mysqld]下加入skip-name-resolve这一句。

MYSQL远程连接速度慢的解决方法

在局域网内连接其他机器的MYSQL,发现速度慢的很,不知道什么原因,总有几秒的延迟.

后来在网上发现解决方法,my.ini里面添加

  [mysqld]
  skip-name-resolve
  skip-grant-tables
Copy after login

这样速度就快了!

 skip-name-resolve
Copy after login

    选项就能禁用DNS解析,连接速度会快很多。不过,这样的话就不能在MySQL的授权表中使用主机名了而只能用ip格式。

    若使用–skip-grant-tables系统将对任何用户的访问不做任何访问控制,但可以用 mysqladmin flush-privileges或mysqladmin reload来开启访问控制;默认情况是show databases语句对所有用户开放,如果mysql服务器没有开远程帐户,就在my.ini里面加上skip-grant-tables

通过以上方法成功解决局域网内mysql数据库连接慢,有需要的朋友可以参考下本篇文章。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

See all articles