---
title: "使用MySQL遇到问题记录"
author: "tvmovie"
date: "2010-05-28T14:28:19Z"
tags: ["MySQL"]
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2014-09-02T14:34:58Z"
id: 677902
url: "https://coffeelover.iteye.com/blog/677902"
---

Q. Ubuntu下从源安装MySQL后，用 mysql -u root 登录时，出现：

```
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
```

*——2010-5-26已解决*

原因：似乎这种安装方式不同于以前的惯例，默认用户root是有密码的，且具体密码不详。

**解决** ： 打开 /etc/mysql/debian.cnf ，可以看到包含如下内容（请忽略password的值）：

```
user     = debian-sys-maint
password = ******
```

   

     用该用户名和密码登录mysql：

```
mysql -u debian-sys-maint -p     
```

 

Q.  Ubuntu下写表时遇到 ERROR 1036: Table 't\_d\_product' is read only

原因：数据库目录或文件的权限问题（由于移动数据 库的文件 等造成）

**解决** ：

1）切换到数据库目录及检查其权限：

```
cd /var/lib/mysql/数据库名称
ls -l
```

 

2）將\*.frm、\*.MYD、\*.MYI 设定权限及使用者mysql：

```
chmod  777  *.*
chown  mysql.mysql  *.*
ls -l
```

 

3）重启数据库：

```
/etc/init.d/mysql stop
/etc/init.d/mysql start
```

*——2010-5-28已解决*

 

Q.  客户端连接时遇到 ERROR 1130: Host '...' is not allowed to connect to this MySQL server

原因：登录帐号不允许从远程登录。

**解决** ：

方法 1）改数据库mysql下的表user中的数据，依次执行：

```
mysql -u root -p
mysql>use mysql;
mysql>update user set host = ‘%’ where user =’test’;
mysql>flush privileges;
mysql>select ‘host’,'user’ from user where user=’test’;
```

 

方法 2）授权法：

如希望用户 test 使用密码 test 从任何主机连接到mysql服务器:  
GRANT ALL PRIVILEGES ON \*.* TO 'test'@'%' IDENTIFIED BY 'test' WITH GRANT OPTION;

 

如想允许用户 test 使用密码 test  从ip为 192.168.1.22 的主机连接到mysql服务器：  
GRANT ALL PRIVILEGES ON \*.* TO 'test'@'192.168.1.22' IDENTIFIED BY ‘test’ WITH GRANT OPTION;

*——2010-10-21已解决*

 

Q.  基于SSH的应用，似乎在每天首次涉及到连接数据库的操作时都会出现如下异常，而之后的操作就没问题了：

 

ERROR \[org.hibernate.util.JDBCExceptionReporter]  
Communications link failure due to underlying exception:

\** BEGIN NESTED EXCEPTION \**

java.net.SocketException  
MESSAGE: Broken pipe

STACKTRACE:

java.net.SocketException: Broken pipe

*——2011-05已解决*

原因：MySQL 的默认设置下，当一个连接的空闲时间超过8小时后，MySQL 就会断开该连接，而连接池则以为该被断开的连接依然有效。在这种情况下，如果客户端代码向连接池请求连接的话，连接池就会把已经失效的连接返回给客户端，客户端在使用该失效连接的时候即抛出异常。

**解决** ：

调整数据库连接配置，增加超时、测试连接等配置

1）如果用的是 c3p0，加上以下配置：

```
   <!-- 数据库连接对象最大持有时间,这个timeout一定要大于MYSQL默认的28800（秒） -->   
   <property name="c3p0.timeout">30000</property>   
   <!-- 最大可缓存数据库语句对象 -->   
   <property name="c3p0.max_statements">50</property>   
   <!-- 每隔28000s测试连接是否可用 -->   
   <property name="c3p0.idle_test_period">28000</property>   
   <property name="c3p0.acquire_increment">1</property> 
```

 参考：[解决java.net.SocketException: Broken pipe异常](http://shshy39.iteye.com/blog/350252 "解决java.net.SocketException: Broken pipe异常")

[【MySql异常备忘】mysql+hibernate出现java.net.SocketException MESSAGE: Broken pipe错误](http://www.blogjava.net/amigoxie/archive/2007/11/26/163114.html)

[java.net.SocketException: Broken pipe /Mysql在经过8小时不使用后会自动关闭已打开的连接](http://dev.firnow.com/course/7_databases/mysql/myxl/20100721/474376.html "java.net.SocketException: Broken pipe /Mysql在经过8小时不使用后会自动关闭已打开的连接")

2）网上大多是用 c3p0 连接池，我用的是 DHCP，看了下 org.apache.commons.dbcp.BasicDataSource 类，发现也有类似的属性，于是加上2项配置：

```
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		...
		<property name="testWhileIdle">
			<value>true</value>
		</property>
		<property name="validationQuery">
			<value>SELECT 1</value>
		</property>
</bean>
```

 加上配置后，跟踪了几天，发现异常果然不再出现，问题解决。

 

Q.  JDBC方式写入datetime类型的字段值（即 java.util.Date 转成 java.sql.Timestamp ）：

```
Timestamp currentTime = new Timestamp(new Date().getTime());
```

 

Q. 2013-6-5 Java程序，原先连接MySQL5.5一切OK，连接MySQL5.6后，有很多操作出现异常：

 

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTION SQL\_SELECT\_LIMIT=DEFAULT' at line 1

**解决** ：

JDBC驱动需要升级，之前用的是5.1.18，改用当前（2013-6-5）最新的 mysql-connector-java-5.1.25 后问题解决。（经试验，只要使用5.1.20及其之后的版本均可以解决这个问题）

 

Q. 2014-4 (mysql-connector-java-5.1.25)

**java.sql.SQLException: Operation not allowed after ResultSet closed** 

com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)  
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)  
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)  
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)  
com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:804)  
com.mysql.jdbc.ResultSetImpl.realClose(ResultSetImpl.java:7331)  
com.mysql.jdbc.ResultSetImpl.close(ResultSetImpl.java:923)  
com.mysql.jdbc.StatementImpl.realClose(StatementImpl.java:2585)  
com.mysql.jdbc.PreparedStatement.realClose(PreparedStatement.java:3158)  
com.mysql.jdbc.StatementImpl.close(StatementImpl.java:577)

**原因：**1个Statement对象关联了多个ResultSet对象，当Statement对象关联给新的ResultSet对象时，上一个ResultSet对象会被关闭，在之后若再使用该ResultSet对象则会出现这个异常。

**解决**：注意多个ResultSet对象的使用顺序。

*see: [http://stackoverflow.com/questions/5840866/getting-java-sql-sqlexception-operation-not-allowed-after-resultset-closed](http://stackoverflow.com/questions/5840866/getting-java-sql-sqlexception-operation-not-allowed-after-resultset-closed)*

[*http://blog.csdn.net/sku0923/article/details/1722370*](http://blog.csdn.net/sku0923/article/details/1722370)

[*http://bugs.mysql.com/bug.php?id=620*](http://bugs.mysql.com/bug.php?id=620)

 

 

Q. 2014-9-1 (mysql-connector-java-5.1.25)

通过JDBC插入数据时(*insert into tableXXX (timestampCol, otherCol) values (null, xxx)*)，数据库中timestamp 类型的默认值无法起效（MySQL5.5下OK，5.6下抛出如下异常）： **com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'xxx' cannot be null**

**原因：**不知道算不算是BUG，在 [MySQL Connector/Net 6.6.5 Maintenance Release](https://blogs.oracle.com/MySqlOnWindows/entry/mysql_connector_net_6_66) 这篇文章中找到 *Fix for Support for current\_timestamp as default for datetime with ef for server 5.6 (Oracle bug #15935094)*. 这段描述。

**解决**：**如果某列值是需要采用数据库中设定的默认值，则不要在 insert 语句中写上该列。**

*注：更换为最新的驱动mysql-connector-java-5.1.32 版本并不能解决该问题。*

另，经测试，得出以下几个情况：

1\) MySQL5.5 数据库端 timestamp 类型的默认值 CURRENT\_TIMESTAMP 起效，无论是否在 insert 语句中设置其值为 NULL;

2\) MySQL5.5 非 timestamp 、设置为 NOT NULL、设有默认值 的列，若 insert 语句中设置其值为 NULL，则会抛出异常 *Column 'xxx' cannot be null*，若在insert 语句中不设值，则数据库中的默认值起效；

3\) MySQL5.6开始支持为 datetime 类型设置默认值 CURRENT\_TIMESTAMP，若 insert 语句中设置其值为 NULL则会抛出异常 *Column 'xxx' cannot be null*，若在insert 语句中不设值，则数据库中的默认值起效。