虚拟主机数据库表损坏_修复实操
数据库表损坏时分清是服务问题还是账号权限问题。所有用户都连不上是服务问题(没启动/端口错/防火墙);只有特定用户连不上是账号权限问题(密码错/主机限制/权限不足)。用root测试区分。
连接配置:网站config文件填主机(localhost/127.0.0.1)、端口(3306)、库名、用户名、密码。localhost走socket,127.0.0.1走TCP,有时localhost连不上换127.0.0.1。远程连接:my.cnf bind-address=0.0.0.0、GRANT授权用户@%、防火墙放行3306。
参考(MySQL管理与密码重置): ``` # 服务管理 systemctl status mysqld systemctl start mysqld systemctl restart mysqld systemctl enable mysqld
# 连接测试 mysql -u用户名 -p密码 -e "SHOW DATABASES;" mysql -u用户名 -p密码 -h 127.0.0.1 -P 3306
# 备份 mysqldump -u用户名 -p密码 库名 > backup.sql mysqldump -u用户名 -p密码 库名 | gzip > backup.sql.gz
# 还原 mysql -u用户名 -p密码 库名 < backup.sql gunzip < backup.sql.gz | mysql -u用户名 -p密码 库名
# 错误日志 tail -50 /var/log/mysqld.log tail -50 /www/server/data/$(hostname).err
# 慢查询 mysql -u root -p -e "SHOW VARIABLES LIKE 'slow_query%';" mysql -u root -p -e "SHOW PROCESSLIST;"
# 重置root密码 systemctl stop mysqld mysqld_safe --skip-grant-tables --skip-networking & mysql -u root -e "USE mysql; UPDATE user SET authentication_string=PASSWORD('新密码') WHERE User='root'; FLUSH PRIVILEGES;" systemctl restart mysqld ```
MySQL性能:慢查询日志(slow_query_log=1, long_query_time=2)找出慢SQL,EXPLAIN分析,加索引;OPTIMIZE TABLE整理碎片;max_connections根据内存设置(每个连接约2MB);innodb_buffer_pool_size设为物理内存50-70%;查询缓存(MySQL 8.0已移除)。优化后查询速度提升3-10倍。
MySQL错误码速查:ERROR 1045 (28000) 密码错或权限错;ERROR 2002 (HY000) 服务未启动或socket错;ERROR 2003 (HY000) 端口或防火墙;ERROR 1049 (42000) 数据库不存在;ERROR 1146 (42S02) 表不存在(表前缀错);ERROR 1064 (42000) SQL语法错;ERROR 1205锁等待超时;ERROR 1040连接数超限;ERROR 2013查询过程中连接丢失。

更新时间:2026-08-27 00:17:01