我有一个VPS,带有2个CPU内核,2 GB RAM,40 GB SSD RAID 10和6个网站,这些网站通常无法连接到MySQL。 其中2个使用InnoDB引擎,而其他两个使用MyISAM。
最常见的错误是
my.cnf文件中的内容是
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
default_authentication_plugin=mysql_native_password
log-error=/var/lib/mysql/server1.nctest.net.err
performance-schema=0
innodb_file_per_table=1
wait_timeout=100
default-storage-engine = MyISAM
max_connections=150
max_allowed_packet=512M
open_files_limit=10000
key_buffer_size=1024M
sort_buffer_size = 10M
read_buffer_size = 12M
read_rnd_buffer_size = 24M
max_heap_table_size=128M
myisam_sort_buffer_size = 256M
myisam_max_sort_file_size=512M
有什么需要添加或删除的东西,以便使数据库性能更好?
我之前也添加了此内容,但是我的数据库无法正常工作,因此我从my.cnf文件中将其删除,并且我的数据库又可以正常工作。
innodb_data_file_path = ibdata1:32M:autoextend
innodb_buffer_pool_size = 768M
innodb_log_file_size = 192M
innodb_log_buffer_size = 64M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 32M
[isamchk]
key_buffer = 512M
sort_buffer_size = 128M
read_buffer = 5M
write_buffer = 8M
[myisamchk]
key_buffer = 512M
sort_buffer_size = 128M
read_buffer = 5M
write_buffer = 5M
[mysqlhotcopy]
interactive-timeout
我也使用MySQLTuner来获取建议,但我还是不理解
# ./mysqltuner.pl
>> MySQLTuner 1.7.15 - Major Hayden <major@mhtx.net>
>> Bug reports, feature requests, and downloads at http://mysqltuner.com/
>> Run with '--help' for additional options and output filtering
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 10.2.23-MariaDB
[OK] Operating on 64-bit architecture
-------- Log file Recommendations ------------------------------------------------------------------
[--] Log file: /var/lib/mysql/server1.nctest.net.err(56M)
[OK] Log file /var/lib/mysql/server1.nctest.net.err exists
[OK] Log file /var/lib/mysql/server1.nctest.net.err is readable.
[OK] Log file /var/lib/mysql/server1.nctest.net.err is not empty
[!!] Log file /var/lib/mysql/server1.nctest.net.err is bigger than 32 Mb
[!!] /var/lib/mysql/server1.nctest.net.err contains 313374 warning(s).
[!!] /var/lib/mysql/server1.nctest.net.err contains 35791 error(s).
[--] 830 start(s) detected in /var/lib/mysql/server1.nctest.net.err
[--] 1) 2019-04-01 22:08:39 139841626527936 [Note] /usr/sbin/mysqld: ready for connections.
[--] 2) 2019-04-01 22:04:37 140567868758208 [Note] /usr/sbin/mysqld: ready for connections.
[--] 3) 2019-04-01 22:01:32 140443240147136 [Note] /usr/sbin/mysqld: ready for connections.
[--] 4) 2019-04-01 21:54:21 139779152431296 [Note] /usr/sbin/mysqld: ready for connections.
[--] 5) 2019-04-01 21:49:33 140472383613120 [Note] /usr/sbin/mysqld: ready for connections.
[--] 6) 2019-04-01 21:44:08 140427658705088 [Note] /usr/sbin/mysqld: ready for connections.
[--] 7) 2019-04-01 21:38:45 140701406279872 [Note] /usr/sbin/mysqld: ready for connections.
[--] 8) 2019-04-01 21:33:54 140239763036352 [Note] /usr/sbin/mysqld: ready for connections.
[--] 9) 2019-04-01 21:31:26 140259203938496 [Note] /usr/sbin/mysqld: ready for connections.
[--] 10) 2019-04-01 21:27:28 140651149260992 [Note] /usr/sbin/mysqld: ready for connections.
[--] 41 shutdown(s) detected in /var/lib/mysql/server1.nctest.net.err
[--] 1) 2019-04-01 16:09:56 139685222135552 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 2) 2019-04-01 15:57:02 139843330197248 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 3) 2019-04-01 14:41:29 140713465878272 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 4) 2019-04-01 8:44:31 139676564809472 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 5) 2019-04-01 7:05:31 140518647924480 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 6) 2019-04-01 6:49:08 139777831921408 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 7) 2019-04-01 5:02:09 140472744417024 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 8) 2019-04-01 4:18:17 140251689785088 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 9) 2019-04-01 2:09:03 140633617929984 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 10) 2019-04-01 2:07:31 140302801114880 [Note] /usr/sbin/mysqld: Shutdown complete
-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +Aria +CSV +InnoDB +MEMORY +MRG_MyISAM +MyISAM +PERFORMANCE_SCHEMA +SEQUENCE
[--] Data in MyISAM tables: 85.1M (Tables: 473)
[--] Data in InnoDB tables: 3.5M (Tables: 17)
[--] Data in MEMORY tables: 0B (Tables: 6)
[OK] Total fragmented tables: 0
-------- Analysis Performance Metrics --------------------------------------------------------------
[--] innodb_stats_on_metadata: OFF
[OK] No stat updates during querying INFORMATION_SCHEMA.
-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[OK] All database users have passwords assigned
[--] There are 618 basic passwords in the list.
-------- CVE Security Recommendations --------------------------------------------------------------
[--] Skipped due to --cvefile option undefined
-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 4m 58s (9K q [33.138 qps], 438 conn, TX: 138M, RX: 2M)
[--] Reads / Writes: 98% / 2%
[--] Binary logging is disabled
[--] Physical Memory : 1.8G
[--] Max MySQL memory : 8.1G
[--] Other process memory: 0B
[--] Total buffers: 1.3G global + 46.5M per thread (150 max threads)
[--] P_S Max memory usage: 0B
[--] Galera GCache Max memory usage: 0B
[OK] Maximum reached memory usage: 1.5G (84.10% of installed RAM)
[!!] Maximum possible memory usage: 8.1G (451.22% of installed RAM)
[!!] Overall possible memory usage with other process exceeded memory
[OK] Slow queries: 0% (0/9K)
[OK] Highest usage of available connections: 3% (5/150)
[OK] Aborted connections: 0.00% (0/438)
[!!] name resolution is active : a reverse name resolution is made for each new connection and can reduce performance
[!!] Query cache may be disabled by default due to mutex contention.
[!!] Query cache efficiency: 0.0% (0 cached / 8K selects)
[OK] Query cache prunes per day: 0
[OK] Sorts requiring temporary tables: 0% (14 temp sorts / 2K sorts)
[OK] No joins without indexes
[!!] Temporary tables created on disk: 83% (1K on disk / 1K total)
[OK] Thread cache hit rate: 98% (5 created / 438 connections)
[OK] Table cache hit rate: 86% (80 open / 92 opened)
[OK] Open file limit used: 1% (147/10K)
[OK] Table locks acquired immediately: 100% (13K immediate / 13K locks)
-------- Performance schema ------------------------------------------------------------------------
[--] Performance schema is disabled.
[--] Memory used by P_S: 0B
[--] Sys schema isn't installed.
-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is enabled.
[--] Thread Pool Size: 2 thread(s).
[--] Using default value is good enough for your version (10.2.23-MariaDB)
-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 38.9% (417M used / 1B cache)
[OK] Key buffer size / total MyISAM indexes: 1.0G/12.1M
[OK] Read Key buffer hit rate: 99.6% (859K cached / 3K reads)
[OK] Write Key buffer hit rate: 100.0% (74 cached / 74 writes)
-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 128.0M/3.5M
[!!] Ratio InnoDB log file size / InnoDB Buffer pool size (75 %): 48.0M * 2/128.0M should be equal to 25%
[OK] InnoDB buffer pool instances: 1
[--] Number of InnoDB Buffer Pool Chunk : 1 for 1 Buffer Pool Instance(s)
[OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances
[OK] InnoDB Read buffer efficiency: 99.69% (143198 hits/ 143648 total)
[!!] InnoDB Write Log efficiency: 0% (3 hits/ 0 total)
[OK] InnoDB log waits: 0.00% (0 waits / 3 writes)
-------- AriaDB Metrics ----------------------------------------------------------------------------
[--] AriaDB is enabled.
[OK] Aria pagecache size / total Aria indexes: 128.0M/1B
[OK] Aria pagecache hit rate: 99.8% (445K cached / 1K reads)
-------- TokuDB Metrics ----------------------------------------------------------------------------
[--] TokuDB is disabled.
-------- XtraDB Metrics ----------------------------------------------------------------------------
[--] XtraDB is disabled.
-------- Galera Metrics ----------------------------------------------------------------------------
[--] Galera is disabled.
-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] Binlog format: MIXED
[--] XA support enabled: ON
[--] Semi synchronous replication Master: Not Activated
[--] Semi synchronous replication Slave: Not Activated
[--] This is a standalone server
-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
/var/lib/mysql/server1.nctest.net.err is > 32Mb, you should analyze why or implement a rotation log strategy such as logrotate!
Control warning line(s) into /var/lib/mysql/server1.nctest.net.err file
Control error line(s) into /var/lib/mysql/server1.nctest.net.err file
MySQL was started within the last 24 hours - recommendations may be inaccurate
Reduce your overall MySQL memory footprint for system stability
Dedicate this server to your database for highest performance.
Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1
When making adjustments, make tmp_table_size/max_heap_table_size equal
Reduce your SELECT DISTINCT queries which have no LIMIT clause
Performance schema should be activated for better diagnostics
Consider installing Sys schema from https://github.com/mysql/mysql-sys for MySQL
Consider installing Sys schema from https://github.com/good-dba/mariadb-sys for MariaDB
Before changing innodb_log_file_size and/or innodb_log_files_in_group read this: https://bit,ly/2TcGgtU
Variables to adjust:
*** MySQL's maximum memory usage is dangerously high ***
*** Add RAM before increasing MySQL buffer variables ***
query_cache_size (=0)
query_cache_type (=0)
query_cache_limit (> 1M, or use smaller result sets)
tmp_table_size (> 16M)
max_heap_table_size (> 128M)
performance_schema = ON enable PFS
innodb_log_file_size should be (=16M) if possible, so InnoDB total log files size equals to 25% of buffer pool size.
===============================
我更新了mysql my.cnf文件。但仍然经常会出现数据库错误。只有当我执行更新帖子或在Wordpress中保存设置时,数据库才会真正崩溃
警告:mysqli_query():MySQL服务器已不在第1767行的 /home/mysite/public_html/wp-includes/db.php中
警告:mysqli_query():在第1767行的 /home/mysite/public_html/wp-includes/db.php中读取结果集的标题时发生错误
重新连接到数据库时出错
我的新#cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
default_authentication_plugin=mysql_native_password
log-error=/var/lib/mysql/server1.nctest.net.err
performance-schema=ON
innodb_file_per_table
innodb_log_file_size=16M
wait_timeout=100
default-storage-engine = MyISAM
max_connections=12
max_allowed_packet=512M
open_files_limit=10000
key_buffer_size=512M
sort_buffer_size = 1M
read_buffer_size = 1M
join_buffer_size = 1M
read_rnd_buffer_size = 24M
max_heap_table_size=1024M
myisam_sort_buffer_size = 48M
myisam_max_sort_file_size=96M
skip-name-resolve=1
query_cache_size=0
query_cache_type=0
query_cache_limit=1M
tmp_table_size=256M
#./mysqltuner.pl
>> MySQLTuner 1.7.15 - Major Hayden <major@mhtx.net>
>> Bug reports, feature requests, and downloads at http://mysqltuner.com/
>> Run with '--help' for additional options and output filtering
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 10.2.23-MariaDB
[OK] Operating on 64-bit architecture
-------- Log file Recommendations ------------------------------------------------------------------
[--] Log file: /var/lib/mysql/server1.nctest.net.err(59M)
[OK] Log file /var/lib/mysql/server1.nctest.net.err exists
[OK] Log file /var/lib/mysql/server1.nctest.net.err is readable.
[OK] Log file /var/lib/mysql/server1.nctest.net.err is not empty
[!!] Log file /var/lib/mysql/server1.nctest.net.err is bigger than 32 Mb
[!!] /var/lib/mysql/server1.nctest.net.err contains 330810 warning(s).
[!!] /var/lib/mysql/server1.nctest.net.err contains 39811 error(s).
[--] 1028 start(s) detected in /var/lib/mysql/server1.nctest.net.err
[--] 1) 2019-04-02 17:19:32 140451092080832 [Note] /usr/sbin/mysqld: ready for connections.
[--] 2) 2019-04-02 17:15:59 139781391579328 [Note] /usr/sbin/mysqld: ready for connections.
[--] 3) 2019-04-02 17:10:00 140566747175104 [Note] /usr/sbin/mysqld: ready for connections.
[--] 4) 2019-04-02 17:04:24 139905996675264 [Note] /usr/sbin/mysqld: ready for connections.
[--] 5) 2019-04-02 17:01:59 140564021029056 [Note] /usr/sbin/mysqld: ready for connections.
[--] 6) 2019-04-02 16:48:20 140572311222464 [Note] /usr/sbin/mysqld: ready for connections.
[--] 7) 2019-04-02 16:47:09 139981523740864 [Note] /usr/sbin/mysqld: ready for connections.
[--] 8) 2019-04-02 16:29:05 140624985798848 [Note] /usr/sbin/mysqld: ready for connections.
[--] 9) 2019-04-02 16:25:39 140484112480448 [Note] /usr/sbin/mysqld: ready for connections.
[--] 10) 2019-04-02 16:24:23 140350801832128 [Note] /usr/sbin/mysqld: ready for connections.
[--] 53 shutdown(s) detected in /var/lib/mysql/server1.nctest.net.err
[--] 1) 2019-04-02 3:51:32 140480395822848 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 2) 2019-04-02 3:48:12 140626773993216 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 3) 2019-04-02 3:25:21 140423948707584 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 4) 2019-04-02 3:23:05 139939032147712 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 5) 2019-04-02 3:12:08 139665156949760 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 6) 2019-04-02 3:04:25 140500729034496 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 7) 2019-04-02 2:43:18 139872808572672 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 8) 2019-04-02 2:33:35 139946881767168 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 9) 2019-04-02 0:35:29 139707099289344 [Note] /usr/sbin/mysqld: Shutdown complete
[--] 10) 2019-04-02 0:32:17 139934613985024 [Note] /usr/sbin/mysqld: Shutdown complete
-------- Storage Engine Statistics -----------------------------------------------------------------
[--] Status: +Aria +CSV +InnoDB +MEMORY +MRG_MyISAM +MyISAM +PERFORMANCE_SCHEMA +SEQUENCE
[--] Data in MyISAM tables: 87.3M (Tables: 473)
[--] Data in InnoDB tables: 3.5M (Tables: 18)
[--] Data in MEMORY tables: 0B (Tables: 6)
[OK] Total fragmented tables: 0
-------- Analysis Performance Metrics --------------------------------------------------------------
[--] innodb_stats_on_metadata: OFF
[OK] No stat updates during querying INFORMATION_SCHEMA.
-------- Security Recommendations ------------------------------------------------------------------
[OK] There are no anonymous accounts for any database users
[OK] All database users have passwords assigned
[--] There are 618 basic passwords in the list.
-------- CVE Security Recommendations --------------------------------------------------------------
[--] Skipped due to --cvefile option undefined
-------- Performance Metrics -----------------------------------------------------------------------
[--] Up for: 1m 43s (1K q [12.971 qps], 25 conn, TX: 14M, RX: 535K)
[--] Reads / Writes: 97% / 3%
[--] Binary logging is disabled
[--] Physical Memory : 1.8G
[--] Max MySQL memory : 1.4G
[--] Other process memory: 0B
[--] Total buffers: 1.0G global + 27.3M per thread (12 max threads)
[--] P_S Max memory usage: 76M
[--] Galera GCache Max memory usage: 0B
[OK] Maximum reached memory usage: 1.1G (63.70% of installed RAM)
[OK] Maximum possible memory usage: 1.4G (78.54% of installed RAM)
[OK] Overall possible memory usage with other process is compatible with memory available
[OK] Slow queries: 0% (0/1K)
[OK] Highest usage of available connections: 16% (2/12)
[!!] Aborted connections: 4.00% (1/25)
[OK] Query cache is disabled by default due to mutex contention on multiprocessor machines.
[OK] Sorts requiring temporary tables: 0% (2 temp sorts / 291 sorts)
[OK] No joins without indexes
[!!] Temporary tables created on disk: 76% (103 on disk / 134 total)
[OK] Thread cache hit rate: 92% (2 created / 25 connections)
[OK] Table cache hit rate: 91% (157 open / 172 opened)
[OK] Open file limit used: 2% (298/10K)
[OK] Table locks acquired immediately: 100% (1K immediate / 1K locks)
-------- Performance schema ------------------------------------------------------------------------
[--] Memory used by P_S: 76.1M
[--] Sys schema isn't installed.
-------- ThreadPool Metrics ------------------------------------------------------------------------
[--] ThreadPool stat is enabled.
[--] Thread Pool Size: 2 thread(s).
[--] Using default value is good enough for your version (10.2.23-MariaDB)
-------- MyISAM Metrics ----------------------------------------------------------------------------
[!!] Key buffer used: 18.6% (99M used / 536M cache)
[OK] Key buffer size / total MyISAM indexes: 512.0M/12.2M
[OK] Read Key buffer hit rate: 98.1% (113K cached / 2K reads)
[!!] Write Key buffer hit rate: 83.3% (90 cached / 75 writes)
-------- InnoDB Metrics ----------------------------------------------------------------------------
[--] InnoDB is enabled.
[--] InnoDB Thread Concurrency: 0
[OK] InnoDB File per table is activated
[OK] InnoDB buffer pool / data size: 128.0M/3.5M
[OK] Ratio InnoDB log file size / InnoDB Buffer pool size: 16.0M * 2/128.0M should be equal to 25%
[OK] InnoDB buffer pool instances: 1
[--] Number of InnoDB Buffer Pool Chunk : 1 for 1 Buffer Pool Instance(s)
[OK] Innodb_buffer_pool_size aligned with Innodb_buffer_pool_chunk_size & Innodb_buffer_pool_instances
[!!] InnoDB Read buffer efficiency: 79.96% (1895 hits/ 2370 total)
[!!] InnoDB Write Log efficiency: 0% (3 hits/ 0 total)
[OK] InnoDB log waits: 0.00% (0 waits / 3 writes)
-------- AriaDB Metrics ----------------------------------------------------------------------------
[--] AriaDB is enabled.
[OK] Aria pagecache size / total Aria indexes: 128.0M/1B
[OK] Aria pagecache hit rate: 99.8% (42K cached / 97 reads)
-------- TokuDB Metrics ----------------------------------------------------------------------------
[--] TokuDB is disabled.
-------- XtraDB Metrics ----------------------------------------------------------------------------
[--] XtraDB is disabled.
-------- Galera Metrics ----------------------------------------------------------------------------
[--] Galera is disabled.
-------- Replication Metrics -----------------------------------------------------------------------
[--] Galera Synchronous replication: NO
[--] No replication slave(s) for this server.
[--] Binlog format: MIXED
[--] XA support enabled: ON
[--] Semi synchronous replication Master: Not Activated
[--] Semi synchronous replication Slave: Not Activated
[--] This is a standalone server
-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
/var/lib/mysql/server1.nctest.net.err is > 32Mb, you should analyze why or implement a rotation log strategy such as logrotate!
Control warning line(s) into /var/lib/mysql/server1.nctest.net.err file
Control error line(s) into /var/lib/mysql/server1.nctest.net.err file
MySQL was started within the last 24 hours - recommendations may be inaccurate
Reduce or eliminate unclosed connections and network issues
Temporary table size is already large - reduce result set size
Reduce your SELECT DISTINCT queries without LIMIT clauses
Consider installing Sys schema from https://github.com/mysql/mysql-sys for MySQL
Consider installing Sys schema from https://github.com/good-dba/mariadb-sys for MariaDB
我如何解决。
答案 0 :(得分:1)
我建议您必须查看手册https://dev.mysql.com/doc/refman/8.0/en/gone-away.html(如果您的MySQL版本小于8.0,则应适用)并在此处收集一些提示。这似乎是导致连接终止的某种超时问题或网络问题。您可以检查是否存在某种防火墙正在限制正在发送的数据包或增加超时变量。例如,您的wait_timeout设置为100秒,只有您的查询运行时间长于100秒,尽管这可能意味着100秒的查询质量不好,除非它拉出了一个本应很少执行的大/长记录列表。查看MySQL文档中* _timeout的默认变量的方式,如果它小于默认值,则尝试对其进行调整。
答案 1 :(得分:1)
只有1.8GB的RAM,有些设置太高了!
max_allowed_packet=512M -- 50M
open_files_limit=10000 -- 1000
key_buffer_size=1024M -- 100M
max_heap_table_size=128M -- 50M
max_heap_table_size=1024M -- really bad!
max_connections=12 -- better
sort_buffer_size = 1M -- better
read_buffer_size = 1M
join_buffer_size = 1M
myisam_sort_buffer_size = 48M -- better
myisam_max_sort_file_size=96M
tmp_table_size=256M -- 50M
innodb_buffer_pool_size = 768M -- 100M
服务器似乎在过度分配内存后迅速重新启动。