調整nginx與msyql效能

(一)調整nginx效能

Step1)找出CPU核心數,筆者核心數為4

# grep ^processor /proc/cpuinfo | wc -l

修改/etc/nginx/nginx.conf

worker_processes 4;

worker_cpu_affinity 0001 0010 0100 1000;

Step2)設定最大開啟檔案數為65536

worker_rlimit_nofile 65535;

Step3)因為筆者使用Linux所以設定為epoll,連線數為65535。

events {

use epoll;

worker_connections 65535;

multi_accept on;

}

Step4)設定最大檔案開啟數為65535,編輯/etc/security/limits.conf,需要重新登入才會啟用

* soft nofile 65535

* hard nofile 65535

查詢目前大檔案開啟數

#ulimit -n

使用指令修改

#ulimit -n 65536


Step5)測試nginx設定檔是否正確

#nginx -t


Step6)重新啟動nginx

#service nginx restart

(二)使用指令ab進行網站壓力測試

Step1)使用指令ab進行效能測試,結果如下。

ab -n 200000 -c 5000 http://localhost/

This is ApacheBench, Version 2.3 <$Revision: 1843412 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/


Benchmarking localhost (be patient)

Completed 20000 requests

Completed 40000 requests

Completed 60000 requests

Completed 80000 requests

Completed 100000 requests

Completed 120000 requests

Completed 140000 requests

Completed 160000 requests

Completed 180000 requests

Completed 200000 requests

Finished 200000 requests



Server Software: nginx/1.18.0

Server Hostname: localhost

Server Port: 80


Document Path: /

Document Length: 26 bytes


Concurrency Level: 5000

Time taken for tests: 9.160 seconds

Complete requests: 200000

Failed requests: 199114

(Connect: 0, Receive: 0, Length: 199114, Exceptions: 0)

Non-2xx responses: 200000

Total transferred: 68257354 bytes

HTML transferred: 33075960 bytes

Requests per second: 21834.29 [#/sec] (mean)

Time per request: 228.998 [ms] (mean)

Time per request: 0.046 [ms] (mean, across all concurrent requests)

Transfer rate: 7277.10 [Kbytes/sec] received


Connection Times (ms)

min mean[+/-sd] median max

Connect: 0 106 276.8 42 3065

Processing: 5 91 92.3 72 1477

Waiting: 2 76 91.3 55 1466

Total: 12 197 291.7 120 4257


Percentage of the requests served within a certain time (ms)

50% 120

66% 136

75% 147

80% 156

90% 319

95% 1109

98% 1157

99% 1174

100% 4257 (longest request)

(三)設定mysql連線數由151增加到1000

Step1)編輯設定檔/etc/mysql/mysql.conf.d/mysqld.cnf

max_connections=1000

Step2)重新啟動mysql服務

#service mysql restart

Step3)確定連線數

#mysql -u root -p

mysql>show variables like '%max_connections%';