数据库错误集锦

一、虚拟机数据库部分

1.1 idea

  • 先使用这边的datasource工具进行连接

在这里插入图片描述

1.2 虚拟机不能通过ip连接虚拟机mysql

  • 未解决

1.3 数据库时区设置

mysql数据库时区配置

1.4 查看当前数据库时间

select now();

1.5 数据库连接依赖

  • mybatis,jpa,jdbc任一依赖
  • mysql-connector-java是必须的

1.6 spring boot数据库连接测试

  • 注意@Test注解引用的包:
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.

远程数据库服务没有登录,可以通过mysql -uroot -p登入

1.6 注意spring boot版本

  • 关于test注解导入
  • 2.2 之前是一个版本,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设置

  • 注意一定要加时区配置
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 注意datasource的包

import javax.sql.DataSource;

1.9 完整测试

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 数据库链接配置

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

二、java部分

2.1Chasret

  • 得到所有的编码格式
 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());
        }
  • 得到对应格式的Charset类

三、mysql

3.1 Host ‘192.168.16.104’ is blocked because of many connection errors; unblock with ‘mysqladmin flush-hosts’

四、常用配置命令

4.1 查看mysql 安装位置命令

 select @@basedir;

4.2 mysql 数据存放命令

 select @@datadir;

4.3 mysql的配置文件与数据在同一目录

在这里插入图片描述

4.4 重启mysql服务

service mysql restart;

4.5 flush-hosts

  • 要在被远程连接的主机上执行
  • 比如在192.168.16.104的电脑上连接192.168.16.103,出现:pymysql.err.OperationalError: (1129, “192.168.16.104’ is blocked because of many connection errors; unblock with ‘mysqladmin flush-hosts’”),则需要在192.168.16.104的电脑上进行:
  • 不能加;
mysqladmin -uroot -p flush-hosts

4.6 设置数据库时区

 set global time_zone = "+00:00";
  • 第二次重新进入mysql才生效
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要查看数据库错误,可以使用以下方法: 1. 查看数据库错误日志:数据库会将错误信息记录在错误日志文件中。您可以通过以下命令来查看 MySQL 数据库错误日志: ``` sudo tail -f /var/log/mysql/error.log ``` 如果您使用的是 MariaDB 数据库,可以使用以下命令来查看错误日志: ``` sudo tail -f /var/log/mariadb/mariadb.log ``` 2. 查看数据库返回的错误信息:在执行 SQL 查询或者操作数据库时,数据库会返回一个错误码和错误信息。您可以在执行 SQL 查询时加上 `SHOW ERRORS` 命令或者在命令行中使用 `mysql_error()` 函数来查看错误信息。 例如,如果您执行了一个错误的查询语句,MySQL 数据库可能会返回以下错误信息: ``` ERROR 1064 (42000): 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 'FROM table_name' at line 1 ``` 这个错误信息告诉我们,查询语句中有一个语法错误,具体的错误位置在 `FROM` 关键字后面。 3. 使用数据库监控工具:一些数据库监控工具可以帮助您实时监控数据库的状态,包括错误信息。例如,可以使用 Nagios、Zabbix 等工具来监控数据库的运行状态,并及时报告错误信息。 总之,要查看数据库错误信息,最简单的方法是查看数据库错误日志文件。如果错误信息不够详细,可以结合 SQL 查询返回的错误信息来分析问题。如果需要更详细的监控和分析功能,可以考虑使用数据库监控工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值