mysql could not be resolved: Name or service not known_MySQL
问题: mysql DNS反解:skip-name-resolve
错误日志有类似警告:
1.120119 16:26:04 [Warning] IP address '192.168.1.10' could not be resolved: Name or service not known
2.120119 16:26:04 [Warning] IP address '192.168.1.14' could not be resolved: Name or service not known
3.120119 16:26:04 [Warning] IP address '192.168.1.17' could not be resolved: Name or service not known
通过show processlist发现大量类似如下的连接:
1.|592|unauthenticated user|192.168.1.10:35320|NULL|Connect| |login|NULL|
2.|593|unauthenticated user|192.168.1.14:35321|NULL|Connect| |login|NULL|
3.|594|unauthenticated user|192.168.1.17:35322|NULL|Connect| |login|NULL|
skip-name-resolve 参数的作用:不再进行反解析(ip不反解成域名),这样可以加快数据库的反应时间。
修改配置文件添加并需要重启:
代码如下:
[mysqld]
skip-name-resolve
其实就是在[mysqld]下面一行加入skip-name-resolve重启mysql服务就可以了。
下面是更加详细的解释:
现象:
程序连接mysql时,mysql的error.log里面提示:
[Warning] IP address '10.0.0.220' could not be resolved: Name or service not known
原因:
Mysql数据库服务器没有配置 /etc/hosts,也没有DNS服务,导致mysqld线程解析IP对应的主机名时,解析失败。
参考资料:
Mysql域名解析:
当一个新的客户端尝试跟mysqld创建连接时,mysqld产生一个新线程来处理这个请求。新线程会先检查请求建立连接的主机名是否在Mysql的主机名缓冲中,如果不在,线程会尝试去解析请求连接的主机名。
解析的逻辑如下:
a. Mysql线程通过gethostbyaddr()把获取的IP地址解析成主机名,然后通过gethostbyname()把获取的主机名解析成IP地址,保障主机名和IP地址对应关系的准确;
b. 如果操作系统支持使用安全进程的gethostbyaddr_r()和gethostbyname_r() 调用,Mysqld线程可以用它俩来优化主机名解析;
c. 如果操作系统不支持安全线程调用,Mysqld进程先做一个互斥锁,然后调用gethostbyaddr()和gethostbyname()解析主机名。此时,在第一个进程释放掉主机名缓冲池的主机名之前,其它进程无法再次解析这个主机名;
在启动mysqld进程是,可以使用 --skip-name-resolve 参数禁用DNS的主机名解析功能,禁用该功能后,在MySQL授权表里面,你只能使用IP地址。
如果你所处环境的DNS非常慢 或者 有很多主机, 你可以通过禁用DNS解析功能--skip-name-resolve 或者 提高 HOST_CACHE_SIZE大小 来提升数据库的响应效率。
禁用主机名缓冲的发方法: 使用--skip-host-cache 参数; 刷新主机名缓冲区: 执行 flush hosts 或者执行mysqladmin flush-hosts;
禁用TCP/IP连接: 使用--skip-networking参数。
实验:
# grep 192.168.1.1 /etc/hosts
192.168.1.1 hostname_online
sql> grant usage on *.* to root@'h_tt_%' identified by 'root';
sql> flush hosts;
# mysql -h 192.168.1.1 -uroot -proot
ERROR 1045 (28000): Access denied for user 'root'@'hostname_online' (using password: YES) ### IP解析为hostname_online,不是h_tt_%,访问被拒。
# grep 192.168.1.1 /etc/hosts
192.168.1.1 hostname_online
192.168.1.1 h_tt_1
# mysql -h 192.168.1.1 -uroot -proot
ERROR 1045 (28000): Access denied for user 'root'@'hostname_online' (using password: YES)#### mysqld没有刷新主机池缓冲池中的IP和主机名信息,此时IP对应hostname_online
sql> flush hosts;
# mysql -h 192.168.1.1 -uroot -proot
ERROR 1045 (28000): Access denied for user 'root'@'hostname_online' (using password: YES) #### mysqld解析了/etc/hosts里面同一个IP对应的第一个主机名关系时,就不再解析后面这个IP对应的主机名关系
# grep 192.168.1.1 /etc/hosts
192.168.1.1 h_tt_1
192.168.1.1 hostname_online
sql> flush hosts;
# mysql -h 192.168.1.1 -uroot -proot
sql> exit
【实验:】验证解析相同IP对应的第一个主机名关系后,就不再解析相同IP:
Sql>grant usage on *.* to root@'h_tt_%' identified by ‘root';
Sql>flush hosts;
# grep h_tt /etc/hosts # grep h_tt /etc/hosts
192.168.1.1hostname_online 192.168.1.1h_tt_1
192.168.1.1h_tt_1 192,168.1.2h_tt_1
访问mysql被拒绝; 从两个IP都可以访问mysql.
【结论】
此实验验证了,上述mysql手册中对"How MySQL Uses DNS"的解释。
即mysqld线程解析/etc/hosts是,是以IP作为唯一标识的,及时一个IP对应了多个主机名,但是mysqld线程只解析第一条对应关系,不论后面有几条这个IP对应的不同主机名的记录,Mysqld进程都不会去解析,都是无效的。
【适用环境:】
没有DNS服务器,主机非常非常多,或者 不想维护/etc/hosts里面手动配置的IP和主机名对应列表时,可以在mysql授权时执行主机名为"%" 或者禁用IP和主机名解析功能(--skip-name-resolve)。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.
