Remember once CentOS7-MySQL course of pit row

First, the cause of the error and

Today the mysql5.7 installed in CentOS7, and then configure the environment in order to test whether the database is successful, he wrote a join operation based java web program mybatis + Spring mysql database, so some of it happens people feel annoying error and stories :

When the program comes to operations on the database such as query, insert and other operations, the first browser to access will be very slow, progress bar has been rotated, then the page will report a 500 error: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache .ibatis.exception. However, I have no problems at CentOS7 connect mysql server and the Windows local Navicat. . .

mysql Dolphins

Second, the process of debugging

1. Check the sql statement

This seems to be looking at the wrong mybatis caused, so check whether xml mapper in sql statement error, such as format conversion parameters, resultType as JavaBean, resultMap need to be defined, and the introduction of JavaBean dao and so on.

Inspection and did not find any problems, but the error persists.

2. MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 报错

Cause Analysis:

View tomcat log file and found there was an error in the beginning of this error. Upon inquiry, I found that the wrong reason is: produce too much (more than mysql database max_connection_errors maximum) interrupted with a database connection ip in a short time caused by congestion.

Solution:

Enter CentOS7 server:

Method a: increase the number of allowed max_connection_errors (palliative):

  1. Mysql database into the view max_connection_errors: show variables like '%max_connection_errors%';
  2. Modify max_connection_errors number is 1000: set global max_connect_errors = 1000;
  3. To see if the amendment is successful:show variables like '%max_connection_errors%';

Method Two: Use the mysqladmin flush-hostscommand clean up the hosts file:

  1. Find the path to mysqladmin:whereis mysqladmin
  2. Execute commands, such as:/usr/local/mysql5.5.35/bin/mysqladmin -uroot -pyourpwd flush-hosts

Note: Method Two clean up the hosts file, you can also go directly to the mysql database execute the command:mysql> flush hosts;

Solve the problem of the hosts, in the tomcat log file, an error was found in the beginning and the emergence of this error:

 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 32 milliseconds ago.  The last packet sent successfully to the server was 32 milliseconds ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
    ......

Cause Analysis:

It indicates that the program failed to communicate with MySQL that connection failed. The last packet successfully received from the server was 32 milliseconds ago mysql represents reconnection, the connection is lost. After this recycling problem for the database connection is idle, the program opens the database connection, while waiting to do database operations, found that MySQL connection is closed out.

I thought it might be connected to wait timeout problem. In mysql configuration database, which is connected latency (the wait_timeout) default is 8 hours. You can view in mysql:

mysql﹥ 
mysql﹥ show global variables like 'wait_timeout'; 
+---------------+---------+ 
| Variable_name | Value | 
+---------------+---------+ 
| wait_timeout | 28800 | 
+---------------+---------+ 
1 row in set (0.00 sec) 

28800 seconds, which is eight hours. If wait_timeout second period, the database connection (the java.sql.Connection) has been in a waiting state, mysql it closed the connection. At this time, Java application connection pool is still legally hold a reference to the connection. When the connection with the database operation, the above error encountered.

MySQL connection once connected the demand will go through six "handshake" before they succeed, any time "handshake" connection failure may result in failure. The first three-way handshake to establish three connections can be simply understood as a necessary TCP handshake, MySQL can not control, more subject to different implementations tcp protocol, followed by three-way handshake timeout and connect_timeout related.

Solution:

Change the database parameter is the simplest approach (wait_timeout modify the value /etc/my.cnf in), but the need to restart the database, a greater impact. Without modifying the database parameters of the premise, you can do has been under treatment:

  • If you are using JDBC, autoReconnect = true in the add jdbc url, such as:dataSource.url=jdbc:mysql://132.231.xx.xxx:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=true&autoReconnect=true

  • If you are using DBCP connection pool in the Spring, the increase in the definition of attributes validationQuery datasource and testOnBorrow, such as:

    <bean id="vrsRankDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${dataSource.driver}"/>
        <property name="url" value="${dataSource.url}"/>
        <property name="username" value="${dataSource.user}"/>
        <property name="password" value="${dataSource.password}"/>
        <property name="validationQuery" value="SELECT 1"/>
        <property name="testOnBorrow" value="true"/>
    </bean>
  • If using a connection pool c3p0 Spring, when the datasource is defined, and adding attribute testConnectionOnCheckin testConnectionOnCheckout, such as:

    <bean name="cacheCloudDB" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${dataSource.driver}"/>
        <property name="jdbcUrl" value="${dataSource.url}"/>
        <property name="user" value="${dataSource.user}"/>
        <property name="password" value="${dataSource.password}"/>
        <property name="initialPoolSize" value="10"/>
        <property name="maxPoolSize" value="10"/>
        <property name="testConnectionOnCheckin" value="false"/>
        <property name="testConnectionOnCheckout" value="true"/>
        <property name="preferredTestQuery" value="SELECT 1"/>
    </bean>

4. Problems slow remote connection Mysql

Try to solve the problem at the above connection timeout, but did not find anything with, or will appear above problems. So I wonder whether a remote connection is too slow Mysql led to the connection timeout? Because I have no problems at CentOS7 connection mysql server and the Windows local Navicat. Check online the next, and found to add the following configuration parameters in the configuration file /etc/my.cnf mysql in:

# 注意该配置是加在[mysqld]下面
[mysqld]
skip-name-resolve

You then need to restart the mysql service. Because according to the instructions, if mysql queries and resolving DNS hosts can cause can cause a slow connection is slow or when there are many client host. Also, please note that the increase in the configuration parameters, host field in the grant tables mysql not be able to use the domain name can only use the ip address, because it is the result of banned domain name resolution.

5. 终极解决:Could not create connection to database server. Attempted reconnect 3 times. Giving up 报错

Cause Analysis:

After the above configuration, re-test the program, it has emerged this error. Upon inquiry, I found that this is due to errors caused by SSL. Because I was using yum command on CentOS7 newly installed MySQL5.7, there is no default configuration of MySQL SSL.

The mysql database on Ubuntu16.04 I've been using, it is the default configuration of MySQL SSL, so I'm used to the url in jdbc connection to the database inside with & useSSL = true, that the use of SSL encryption. Cause when I connected the current mysql, has not connect. Tomcat log to see the end of an error, an error will be as follows:

Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1946)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:316)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:310)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1639)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:223)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1037)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:965)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1064)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
    at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:186)
    ... 24 more
Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors
    at com.mysql.jdbc.ExportControlled$X509TrustManagerWrapper.checkServerTrusted(ExportControlled.java:302)
    at sun.security.ssl.AbstractTrustManagerWrapper.checkServerTrusted(SSLContextImpl.java:1091)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1621)
    ... 32 more
Caused by: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors
    at sun.security.provider.certpath.PKIXCertPathValidator.validate(PKIXCertPathValidator.java:154)
    at sun.security.provider.certpath.PKIXCertPathValidator.engineValidate(PKIXCertPathValidator.java:80)
    at java.security.cert.CertPathValidator.validate(CertPathValidator.java:292)
    at com.mysql.jdbc.ExportControlled$X509TrustManagerWrapper.checkServerTrusted(ExportControlled.java:295)
    ... 34 more

Then I understood why. . .

Solution:

Used when configuring the JDBC URL & useSSL = false, i.e., without the use of SSL encryption, normal connection is configured:

dataSource.url = jdbc:mysql://132.231.xx.xxx:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&autoReconnect=true

If the higher data security proposal is still in the SSL coupled with MySQL end.

Third, the summary

Because MySQL SSL issues caused a series of problems. Because SSL encryption problem caused the program to mysql connection timeout, and then been sent to mysql connection, leading to the same ip blocking too many interruptions in the production database connections resulting in a short time.

After seeing the error log, in addition to watching the very beginning of error, we must also look at the end of an error, the fix will be some other findings.


PS: If you think something is wrong article, where poorly written, or have any suggestions, please advice.

Welcome to your thumbs, collection and comment!
(Finish)

Guess you like

Origin www.cnblogs.com/deecyn/p/11442319.html