本文转载自 GitHub 博客,作者是 张甦 。今天给大家列举 MySQL 数据库中最经典的九大错误案例,并附有处理问题的解决思路和方法。希望能给刚入行或数据库爱好者一些帮助,今后再遇到任何报错,我们都可以很淡定地去处理。
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.132 ERROR 1040 (00000): Too many connections
Last_Errno: 1062 Last_Error: Could not execute Write_rows event on table test.t;Duplicate entry ‘4’ for key ‘PRIMARY’, Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY;the event’s master log mysql-bin.000014, end_log_pos 1505针对这个报错,我们首先要考虑是不是在从库中误操作导致的。结果发现,我们在从库中进行了一条针对有主键表的SQL语句的插入,导致主库再插入相同 sql 的时候,主从状态出现异常。发生主键冲突的报错。
[root@zs bin]# ./pt-slave-restart -uroot -proot123
2017-07-20T14:05:30 p=…,u=root node4-relay-bin.000002 1506 1062
Last_IO_Errno: 1593(server-id冲突)这个报错出现之后,就能一目了然看到两台机器的 server-id是一样的。 在搭建主从复制的过程中,我们要确保两台机器的 server-id是唯一的。这里再强调一下 server-id的命名规则(服务器 ip 地址的最后一位+本 MySQL 服务的端口号)。
Last_IO_Error: Fatal error: The slave I/O thread stops because master andslave have equalMySQL server ids; these ids must be different forreplication towork (orthe–replicate-same-server-id option must be used on slave butthis doesnotalways make sense; please check themanual beforeusing it)
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
/usr/local/mysql/bin/mysqlbinlog –no-defaults -v -v –-output=decode-rows /data/mysql/mysql-bin.000014 |grep -A 10 1708 > 1.log获取到SQL语句之后,就可以在从库反向执行SQL语句。把从库缺少的SQL语句补全,解决报错信息。
cat 1.log
#170720 14:20:15 server id 3 end_log_pos 1708 CRC32 0x97b6bdec Update_rows: table id 113 flags: STMT_END_F ### UPDATE `test`.`t` ### WHERE ### @1=4 /* INT meta=0 nullable=0 is_null=0 */ ### @2=’dd’ /* VARSTRING(60) meta=60 nullable=1 is_null=0 */ ### SET ### @1=4 /* INT meta=0 nullable=0 is_null=0 */ ### @2=’ddd’ /* VARSTRING(60) meta=60 nullable=1 is_null=0 */ # at 1708 #170720 14:20:15 server id 3 end_log_pos 1739 CRC32 0xecaf1922 Xid = 654 COMMIT/*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
mysql> insert into t (b) values (‘ddd’); Query OK, 1row affected (0.01 sec) mysql> stop slave; Query OK,0rows affected (0.00 sec) mysql> exit Bye [root@node4 bin]# ./pt-slave-restart -uroot -proot123 2017-07-20T14:31:37 p=…,u=root node4-relay-bin.0000052831032
[root@zs data]# /usr/local/mysql/bin/mysqld_safe –defaults-file=/etc/my.cnf &[1] 3758
[root@zs data]#17072014:41:24mysqld_safe Logging to ‘/data/mysql/error.log’.
17072014:41:24mysqld_safe Starting mysqld daemon with databases from /data/mysql170720
14:41:25mysqld_safe mysqld from pid file/data/mysql/node4.pid ended
17072014:41:24mysqld_safe Starting mysqld daemon with databases from /data/mysql2017-07-20
14:41:250[Warning] TIMESTAMP with implicitDEFAULTvalueis deprecated.
Pleaseuse–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
[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@zsdata]# 170720 14:45:56 mysqld_safe Logging to ‘/data/mysql/error.log’. 17072014:45:56mysqld_safe Starting mysqld daemon with databases from /data/mysql
./mysql_install_db –basedir=/usr/local/mysql/ –datadir=/data/mysql/–defaults-file=/etc/my.cnf –user=mysql
[root@zs ~]# mysql -uroot -p Enter password: ERROR1045(28000): Access denied foruser ‘root’@’localhost’ (usingpassword: YES) [root@zs ~]# mysql -uroot -p Enter password: ERROR 1045(28000): Access deniedforuser ‘root’@’localhost’ (usingpassword: YES)
/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’;
CREATE TABLE `t` ( `a` int(11) NOT NULL AUTO_INCREMENT, `b` varchar(20) DEFAULT NULL, PRIMARY KEY (`a`), KEY `b` (`b`) ) ENGINE=InnoDB AUTO_INCREMENT=300 DEFAULT CHARSET=utf8
mysql> insert into t (b) values (‘aa’);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t (b) values (‘bb’);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t (b) values (‘cc’);
Query OK, 1 row affected (0.00 sec)
mysql> select *from t;
+—–+——+
|a |b |+—–+
——+ |300 |aa |
|301 |bb |
|302 |cc |
+—–+——+
3 rows in set (0.00 sec)
Caused by: java.sql.SQLException: Incorrect string value: ‘😗🅒 forcolumn ‘CONTENT’ at row 1at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
vim /etc/my.cnf
[mysqld] init-connect=’SET NAMES utf8mb4′ character-set-server=utf8mb4注:utf8mb4 是 utf8 的超集。
org.hibernate.util.JDBCExceptionReporter – SQL Error:0, SQLState: 08S01
org.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 value of ‘wait_timeout’. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection ‘autoReconnect=true’ to avoid this problem. org.hibernate.event.def.AbstractFlushingEventListener – Could not synchronize database state with session org.hibernate.exception.JDBCConnectionException: Could not execute JDBC batch update com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state. org.hibernate.util.JDBCExceptionReporter – SQL Error:0, SQLState: 08003 org.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 code24: Too manyopenfiles超出最大打开文件数限制!ulimit -n 查看系统的最大打开文件数是 65535,不可能超出!那必然是数据库的最大打开文件数超出限制!
来源:运维派,作者:张甦
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。