记一次性能优化点

最近学习了个小项目, 打算把它作为我们队的论坛系统,但部署到服务器上时发现 打开一个页面要等好久。在此记录下如何解决的,最后的处理结果也是非常简单。

线上地址

项目地址

部署

这个项目是用Laravel框架,本地开发使用Homestead,开发起来非常方便。线上部署使用了Laradock比较好用的一个Docker化的一个PHP运行环境

因为某个还未解决的问题,没有使用Laradock里的MySQL容器,我就想着使用另外一台服务器运行MySQL,当整个部署完成后,MySQL远程连接开启。启动项目:)

问题展现

。。。。怎么有点慢,打开 Laravel Debugbar,发现页面加载耗时10S(……),再次查看,发现每次请求,第一条sql耗时将近10S,后面的sql语句还算正常。

问题解决

通过查询相关资料,原来是MySQL还要进行DNS解析,这就好办了禁用DNS解析就行通过在mysql的配置文件里添加

1
2
[mysqld]
skip-name-resolve

如果禁用DNS查询MySQL怎么知道远程连接的客户端在哪个位置呢,这个也好解决,只要在配置MySQL远程连接时指定一个固定的IP就行(必须使用IP地址)。

之后再打开应用就快很多了。

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.

感谢土豪
0%