Ubuntu 16.4 和 PHP 性能问题

Ubuntu 16.4 和 PHP 性能问题

我们在 Windows Server 2016 环境中新安装了 Ubuntu 16.4 LTS + apache + mysql 虚拟服务器。

我们目前在云服务上的生产系统运行正常,服务器上的页面加载时间约为 200 毫秒左右。但在我们的环境中使用时,同一页面的加载时间约为 3、4 秒。这只发生在一个特定的 PHP 页面上。其他页面的页面加载时间结果还算不错。

服务器有大量可用资源,我已为服务器分配了 40 个核心(全部)和 8 GB 的 RAM。因此,这应该不是由于资源不足造成的。

到目前为止我已经调整了 mysql。如果我直接使用 phpmyadmin 运行 SQL 查询,它们的响应时间大约为 0.02 秒/查询...

我认为我们的环境中有某些东西阻止了 PHP 脚本或某些与网络相关的东西,或者 PHP、Apache 设置有问题...

从哪里开始调试这个问题?如何加速 PHP?我在 Google 上搜索了很多次,但都无济于事。例如不要调用 mysql localhost,而是使用 127.0.0.1 等等。

答案1

如果你从配置文件中删除以下几行,

sort_buffer_size
read_buffer_size
read_rnd_buffer_size

系统默认设置将为您工作,并避免对每个新连接有害的大量 RAM 分配。使用 MySQLCalculator.com 将有助于您理解与每个连接的这 3 个关键 RAM 资源相关的答案。

答案2

主要问题在于 mysql 配置。其中一些设置对 mysql 性能至关重要。当前设置:

    #
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld_safe]
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
#
# * Basic Settings
#
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 0.0.0.0
#
#omat saadot
key_buffer_size = 128M
#kopioitu vanhasta
sort_buffer_size = 12M
read_buffer_size = 12M
read_rnd_buffer_size = 12M
myisam_sort_buffer_size = 76M
query_cache_type=1

thread_stack = 128K
max_heap_table_size = 256M
query_cache_limit = 1M
query_cache_size = 16M

innodb_log_file_size = 512M

#kopioituja vanhasta
# In theory, you want this to encompass your entire database. In
# practice, you only have so much RAM available
innodb_buffer_pool_size = 512M
# Reduce contention. Set to 4+ in MySQL 5.5+
innodb_buffer_pool_instances = 4
# O_DIRECT bypasses the operating system's handling of data. This can
# drastically improve how well your system handles large amounts of RAM by
# removing buffers (once in InnoDB's cache, again in the filesystem's
# cache), at a slight cost to reliability. This appears to be more dramatic
# the more RAM you have. Should reduce swap pressure and in most cases
# improves performance.
#innodb_flush_method = O_DIRECT
# Control Durability
# 1=flush and sync; 2=flush; 0=neither
# Setting innodb_flush_log_at_trx_commit to 0 causes InnoDB to only flush to
# disk once per second, improving performance considerably.
innodb_flush_log_at_trx_commit = 0
# How long to wait for row level locks before bailing out.
innodb_lock_wait_timeout = 180
# The number of open tables for all threads. Increasing this value increases
# the number of file descriptors that mysqld requires.
#table_cache = 4096
# The next two lines replace the basic table_cache value as of MySQL 5.1.
# table_definition_cache should be big enough for every table in your database,
# plus temporary tables, and table_open_cache should be a reflection of how
# many of these will be open in a live connection at once - it will likely
# exceed your definition cache.  It doesn't hurt to set these to large values.
# They don't take a lot of RAM and it's better than hitting the limit.
table_definition_cache = 4096
table_open_cache = 16384
# The default optimizer_search_depth is 62. This causes MySQL to take
# an obscene amount of time planning a query, though when it finally
# executes, it is pretty close to optimal.
optimizer_search_depth = 32
#open-files = 10000




skip_name_resolve

# * Fine Tuning
#
key_buffer_size     = 16M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit   = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
slow_query_log=1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 1
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id      = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size   = 100M
#binlog_do_db       = include_database_name
#binlog_ignore_db   = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem


sql-mode="TRADITIONAL"

我认为,先前的设置是 mysql 安装后和首次启动后的默认设置:

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld_safe]
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
#
# * Basic Settings
#
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 0.0.0.0
#
# * Fine Tuning
#
key_buffer_size     = 16M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit   = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries   = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id      = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size   = 100M
#binlog_do_db       = include_database_name
#binlog_ignore_db   = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

我不确定什么是最重要的?我是否应该进一步提高这些值?我确实有很多内存可以使用。目前,我在这个虚拟机上有 8 GB 的内存。

我想知道是什么原因导致 mysql 出现如此大的差异?您对生产中的 Mysql 有一些首选设置吗?

相关内容