Database error collection
- 1. Virtual machine database part
-
- 1.1 idea
- 1.2 The virtual machine cannot connect to the virtual machine mysql through IP
- 1.3 Database time zone setting
- 1.4 Check the current database time
- 1.5 Database connection dependencies
- 1.6 spring boot database connection test
- 1.6 Pay attention to the spring boot version
- 1.7 mybatis-plus settings
- 1.8 Pay attention to the datasource package
- 1.9 Complete testing
- 1.10 Database link configuration
- 2. Java part
- 3. mysql
- 4. Commonly used configuration commands
1. Virtual machine database part
1.1 idea
- First use the datasource tool here to connect
1.2 The virtual machine cannot connect to the virtual machine mysql through IP
- unsolved
1.3 Database time zone setting
mysql database time zone configuration
1.4 Check the current database time
select now();
1.5 Database connection dependencies
- Mybatis, jpa, jdbc any dependency
- mysql-connector-java is required
1.6 spring boot database connection test
- Note the package referenced by the @Test annotation:
import org.junit.jupiter.api.Test;
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.SQLException;
@SpringBootTest
class DemoApplicationTests {
@Autowired
DataSource dataSource;
@Test
public void testMysqlConn() throws SQLException {
System.out.println(dataSource.getConnection());
}
}
- 报错:The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
The remote database service is not logged in, you can log in through mysql -uroot -p
1.6 Pay attention to the spring boot version
- About test annotation import
- There was one version before 2.2, and a different version after 2.2.
在Spring Boot 2.2.X以后使用import org.junit.jupiter.api.Test Junit5
在Spring Boot 2.2.x之前使用import org.junit.Test Junit4
1.7 mybatis-plus settings
- Note that you must add time zone configuration
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/shopping?serverTimezone=GMT%2B8
username: root
password: ******
#mybatis输出日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
1.8 Pay attention to the datasource package
import javax.sql.DataSource;
1.9 Complete testing
package com.ybx.sunshine;
import com.ybx.sunshine.entity.User;
import com.ybx.sunshine.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.SQLException;
@SpringBootTest
public class Usertest {
@Autowired
UserMapper userMapper;
@Test
public void testUser(){
User user = userMapper.selectById(1);
System.out.println(user.getName());
}
@Autowired
DataSource dataSource;
@Test
public void testMysqlConn() throws SQLException {
System.out.println(dataSource.getConnection());
}
}
1.10 Database link configuration
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://localhost:3306/shopping?serverTimezone=GMT%2B8
url: jdbc:mysql://localhost:3306/shopping?serverTimezone=GMT%2B8
username: root
password: ******
#mybatis输出日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2. Java part
2.1Chasret
- Get all encoding formats
SortedMap<String, Charset> stringCharsetSortedMap = Charset.availableCharsets();
Set<Map.Entry<String, Charset>> entries = stringCharsetSortedMap.entrySet();
for(Map.Entry<String, Charset> key : entries){
System.out.println(key.getKey() + " " + key.getValue());
}
- Get the Charset class of the corresponding format
3. mysql
3.1 Host ‘192.168.16.104’ is blocked because of many connection errors; unblock with ‘mysqladmin flush-hosts’
4. Commonly used configuration commands
4.1 View mysql installation location command
select @@basedir;
4.2 mysql data storage command
select @@datadir;
4.3 The mysql configuration file and data are in the same directory
4.4 Restart the mysql service
service mysql restart;
4.5 flush-hosts
- To be executed on the remotely connected host
- For example, when connecting to 192.168.16.103 on the computer of 192.168.16.104, pymysql.err.OperationalError: (1129, "192.168.16.104' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'"), you need to Do this on the computer at 192.168.16.104:
- Cannot be added;
mysqladmin -uroot -p flush-hosts
4.6 Set database time zone
set global time_zone = "+00:00";
- It will only take effect after re-entering mysql for the second time.