今天就给大家列举 MySQL 数据库中,最经典的十大错误案例,并附有处理问题的解决思路和方法,希望能给刚入行,或数据库爱好者一些帮助,今后再遇到任何报错,我们都可以很淡定地去处理。学习任何一门技术的同时,其实就是自我修炼的过程。沉下心,尝试去拥抱数据的世界!
Top 1:Too many connections(连接数过多,导致连接不上数据库,业务无法正常进行)
问题还原
mysql> show variables like '%max_connection%';| Variable_name | Value |max_connections | 151 | mysql> set global max_connections=1;Query OK, 0 rows affected (0.00 sec)[root@node4 ~]# mysql -uzs -p123456 -h 192.168.56.132ERROR 1040 (00000): Too many connections
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it)#这个报错出现之后,就看一目了然看到两台机器的 server-id 是一样的。
在搭建主从复制的过程中,我们要确保两台机器的 server-id 是唯一的。这里再强调一下 server-id 的命名规则(服务器 ip 地址的最后一位+本 MySQL 服务的端口号)解决方法:在主从两台机器上设置不同的 server-id。Last_SQL_Errno: 1032(从库少数据,主库更新的时候,从库报错)
Last_SQL_Error:Could not execute Update_rows event on table test.t; Can't find record in 't', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000014, end_log_pos 1708
[root@zs data]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &[1] 3758[root@zs data]# 170720 14:41:24 mysqld_safe Logging to '/data/mysql/error.log'.17072014:41:24 mysqld_safe Starting mysqld daemon with databases from /data/mysql170720 14:41:25 mysqld_safe mysqld from pid file /data/mysql/node4.pid ended170720 14:41:24 mysqld_safe Starting mysqld daemon with databases from /data/mysql2017-07-20 14:41:25 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details)./usr/local/mysql/bin/mysqld: File '/data/mysql/mysql-bin.index' not found (Errcode: 13-Permission denied)2017-07-2014:41:254388 [ERROR] Aborting
解决思路:遇到这样的报错信息,我们要学会时时去关注错误日志 error log 里面的内容。看见了关键的报错点Permission denied。证明当前 MySQL 数据库的数据目录没有权限。解决方法:
[root@zs data]# chown mysql:mysql -R mysql[root@zs data]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &[1] 4402[root@zs data]# 170720 14:45:56 mysqld_safe Logging to '/data/mysql/error.log'.17072014:45:56 mysqld_safe Starting mysqld daemon with databases from /data/mysql#启动成功。
[root@zs~]# mysql -uroot -pEnter password: ERROR 1045 (28000): Access denied foruser'root'@'localhost' (using password: YES)[root@zs~]# mysql -uroot -pEnter password: ERROR 1045 (28000): Access denied foruser'root'@'localhost' (using password: YES)#我们有可能刚刚接手别人的 MySQL 数据库,而且没有完善的交接文档,公众号:Java精选。root 密码可以丢失或者忘记了。
解决思路:目前是进入不了数据库的情况,所以我们要考虑是不是可以跳过权限。因为在数据库中,mysql数据库中user表记录着我们用户的信息。解决方法:启动 MySQL 数据库的过程中,可以这样执行:
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &这样启动,就可以不用输入密码,直接进入 mysql 数据库了。然后在修改你自己想要改的root密码即可。update mysql.user set password=password('root123') where user='root';
Top 5:truncate删除数据,导致自动清空自增ID,前端返回报错not found。
这个问题的出现,就要考虑下truncate 和 delete 的区别了。看下实验演练:
#首先先创建一张表;CREATETABLE `t` ( `a` int(11) NOTNULL AUTO_INCREMENT, `b` varchar(20) DEFAULTNULL, PRIMARY KEY (`a`), KEY `b` (`b`)) ENGINE=InnoDB AUTO_INCREMENT=300DEFAULT CHARSET=utf8#插入三条数据:mysql>insertinto t (b) values ('aa');Query OK, 1row affected (0.00 sec)mysql>insertinto t (b) values ('bb');Query OK, 1row affected (0.00 sec)mysql>insertinto t (b) values ('cc');Query OK, 1row affected (0.00 sec)mysql>select*from t;+-----+------+| a | b |+-----+------+| 300 | aa || 301 | bb || 302 | cc |+-----+------+3 rows in set (0.00 sec)#先用 delete 进行删除全表信息,再插入新值。
结果发现truncate把自增初始值重置了,自增属性从1开始记录了。当前端用主键id进行查询时,就会报没有这条数据的错误。个人建议不要使用truncate对表进行删除操作,虽然可以回收表空间,但是会涉及自增属性问题。这些坑,我们不要轻易钻进去。Top 6:阿里云 MySQL 的配置文件中,需要注意一个参数设置就是:
vim /etc/my.cnf[mysqld]init-connect='SET NAMES utf8mb4'character-set-server=utf8mb4注:utf8mb4 是 utf8 的超集。
Top 8:使用 binlog_format=statement 这种格式,跨库操作,导致从库丢失数据,用户访问导致出现错误数据信息。
#当前数据库二进制日志的格式为:binlog_format=statement在主库设置binlog-do-db=mydb1(只同步mydb1这一个库)在主库执行use mydb2;insert into mydb1.t1 values ('bb');这条语句不会同步到从库。但是这样操作就可以;use mydb1;insert into mydb1.t1 values ('bb');因为这是在同一个库中完成的操作。#在生产环境中建议使用binlog的格式为row,而且慎用binlog-do-db参数。
Top 9:MySQL 数据库连接超时的报错
org.hibernate.util.JDBCExceptionReporter -SQL Error:0, SQLState: 08S01org.hibernate.util.JDBCExceptionReporter - The last packet successfully received from the server was43200 milliseconds ago.The last packet sent successfully to the server was 43200 milliseconds ago, which is longer than the server configured valueof'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured valuesfor client timeouts, orusing the Connector/J connection 'autoReconnect=true'to avoid this problem.org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with sessionorg.hibernate.exception.JDBCConnectionException: Could notexecute JDBC batch updatecom.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.org.hibernate.util.JDBCExceptionReporter -SQL Error:0, SQLState: 08003org.hibernate.util.JDBCExceptionReporter -No operations allowed after connection closed. Connection was implicitly closed due to underlying exception/error: **BEGIN NESTED EXCEPTION **
linux:/usr/local/mysql/bin # ./perror 24OS error code 24: Too many open files
超出最大打开文件数限制!ulimit -n查看系统的最大打开文件数是65535,不可能超出!那必然是数据库的最大打开文件数超出限制!在 MySQL 里查看最大打开文件数限制命令:show variables like 'open_files_limit';发现该数值过小,改为2048,重启 MySQL,应用正常处理方法:
repair table ;chown mysql权限#清理磁盘中的垃圾数据
今后还会继续总结 MySQL 中的各种报错处理思路与方法,希望跟各位老铁们,同学们一起努力。多沟通多交流!
今天就给大家列举 MySQL 数据库中,最经典的十大错误案例,并附有处理问题的解决思路和方法,希望能给刚入行,或数据库爱好者一些帮助,今后再遇到任何报错,我们都可以很淡定地去处理。学习任何一门技术的同时,其实就是自我修炼的过程。沉下心,尝试去拥抱数据的世界!
Top 1:Too many connections(连接数过多,导致连接不上数据库,业务无法正常进行)
Top 2:主从复制报错类型
Top 3:MySQL安装过程中的报错
Top 4:数据库密码忘记的问题
Top 5:truncate删除数据,导致自动清空自增ID,前端返回报错not found。
Top 7:数据库总会出现中文乱码的情况
Top 8:使用 binlog_format=statement 这种格式,跨库操作,导致从库丢失数据,用户访问导致出现错误数据信息。
Top 9:MySQL 数据库连接超时的报错
Top 10 :can't open file (errno:24)
汇集2025年讨论度最高的数据库议题,XCOPS智能运维管理人年会将于5月16日在广州举办。大会精选金融核心系统数据库切换、多模态数据库设计、存算分离架构搭建,以及云原生数据库、数仓及数据湖的创新实践等干货案例,就等你扫码一起来探讨↓