数据库 - 数据库问题汇总

数据库问题汇总

1.util.Date类型数据插入mysql时数据库时,日期会少13小时

在连接参数中添加“serverTimezone=Asia/Shanghai”。

2.定义band字段在 Mybatis中关于OGNL表达式会出现冲突:Malformed OGNL expression: band != null

表字段为band,band是捆绑的意思,与mybatis的OGNL表达式发生冲突。可能发生冲突的变量集合

  • bor  字符|
  • xor      字符^
  • and      字符&&
  • band    字符&
  • eq       字符==
  • neq     字符!=
  • lt        字符<
  • gt       字符>
  • lte       字符<=
  • gte     字符>=
  • shl     字符 <<
  • shr     字符>>
  • ushr    字符>>>

3.com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.QRTZ_TRIGGERS' doesn't exist

问题:数据库中明这张表 qrtz_triggers 还是报这个错

原因:这个错报的是在数据库test下面没有这个表【QRTZ_TRIGGERS】。其实数据库中有的是【qrtz_triggers】。区别在于一个是大写,一个是小写。默认情况下,mysql是区分大小写的,所以为了避免这种问题,就需要把 mysql 的区分大小写的属性给修改了。

解决办法:

用root登录,修改 /etc/my.cnf。在[mysqld]节点下,加入一行: lower_case_table_names=1

CentOS 7下mysql停止和重启:

/bin/systemctl stop mysqld.service

/bin/systemctl restart mysqld.service

4.各数据库表名和字段名长度限制

5.mysql修改端口号

登录mysql:

mysql -u root -p

查看当前端口:

show global variables like 'port';

修改 my.cnf 配置文件的 port 属性:

port=要修改的端口号

重启mysql:

service mysqld restart 

6.UNION ALL为什么会降低性能

High Performance MySQL, Second Edition P195

Optimizing UNION
MySQL always executes UNION queries by creating a temporary table and filling it
with the UNION results. MySQL can’t apply as many optimizations to UNION queries as
you might be used to. You might have to help the optimizer by manually “pushing down” 
WHERE, LIMIT, ORDER BY, and other conditions (i.e., copying them, as appropri-
ate, from the outer query into each SELECT in the UNION).
It’s important to always use UNION ALL, unless you need the server to eliminate dupli-
cate rows. If you omit the ALL keyword, MySQL adds the distinct option to the tem-
porary table, which uses the full row to determine uniqueness. This is quite
expensive. Be aware that the ALL keyword doesn’t eliminate the temporary table,
though. MySQL always places results into a temporary table and then reads them
out again, even when it’s not really necessary (for example, when the results could be
returned directly to the client).

union 会生成临时表,整个过程就相当于是先写再读,所以在某些情况下速度会变慢很多。

7.mybatis中大于等于小于等于的写法

大于等于
<![CDATA[ >= ]]>
小于等于
<![CDATA[ <= ]]>
例如:sql如下:
create_date_time <![CDATA[ >= ]]> #{startTime} and  create_date_time <![CDATA[ <= ]]> #{endTime}

8.MySQL查询和修改表自增值

查询:

SELECT Auto_increment FROM information_schema.TABLES WHERE Table_Schema= '数据库名' AND table_name= '表名';

修改:

alter table 表名 auto_increment=新的自增值

9.where语句中的时间范围查询不走索引

17%

当查询的数据占总表数据的17%以上时,才会走索引,否则会走全表扫描。这也是不建议在区分度小的列上创建索引的原因。

10.计算MySQL TPS和QPS

QPS = Questions(or Queries) / seconds

show  global  status like 'Question%'; 

间隔seconds秒执行两次以上命令,然后(结果2-结果1)/seconds 就是QPS

TPS = (Com_commit + Com_rollback) / seconds 

show global status like 'Com_commit'; 
show global status like 'Com_rollback'; 

算法与QPS计算相同。

11.invalid comparison: java.util.Date and java.lang.String异常

数据库定义的createDate 是datetime类型,实体类中定义了

private Date date;

mybatis查询:

<if test="date!= null and date !=''">
    AND date_format(har.create_date,'%Y-%m-%d') < date_add(date_format(#{date},'%Y-%m-%d'),interval 1 day)
 </if>

执行时抛出异常invalid comparison: java.util.Date and java.lang.String,原因:

<if test="date!= null and date !=''">

date为Date类型,不能和 ‘’ 比较,只判断是不是null就行了:

<if test="date!= null">

12.输入net start mysql 出现的几种错误及解决方法

发生系统错误:

 错误原因:没有获取管理员权限。

解决方法:win10系统可以

快捷键 win+q 打开搜索栏 -> 输入cmd -> 对命令提示符右键 -> 打开文件位置 -> 选择命令提示符的快捷方式 -> 右键打开属性 -> 高级 -> 用管理员身份运行。

这样以后打开 cmd 就都是管理员身份的了。

服务名无效:

错误原因:net start + 服务名,启动的是win下注册的服务。报这个错说明 mysql 还没有注册到 win 中

解决方法:切换到 mysql 安装路径下的 bin 文件夹,在命令行中输入 mysqld --install。 如果出现"Remove of the Service Denied!",只需要以管理员身份运行 cmd,然后执行 mysqld --install。

13.SQL Server 设置用户默认schema失效

SQL Server 创建登录名时,默认的 server 权限是 pubilc 和 sysadmin。把sysadmin去掉后,问题解决。

14.数据库插入数据报错:传入的请求具有过多的参数。该服务器支持最多 2100 个参数。请减少参数的数目,然后重新发送该请求

原因是在sql语句中,一次向数据库传入参数超出限制(比如用了mybatis的foreach)。通过分批向数据库插入即可

int count = 90;
int batch = list.size() / count;
if (list.size() % count != 0) {
    batch = batch + 1;
}

//循环批量保存每组数据
for (int i = 0; i < batch; i++) {
    List<DemoEntity> subList = null;
    if (i == batch - 1) {
        subList = list.subList(count * i, list.size());
    } else {
        subList = list.subList(count * i, count * (i + 1));
    }
    JDBCMapper.addData(subList);
}

15.SQL Server 连接报错:The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL)

报错如下:

ERROR com.alibaba.druid.pool.DruidDataSource:936 - init datasource error,
url: jdbc:sqlserver://localhost:1433;DatabaseName=xxx;integratedSecurity=false
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. 
Error: "Server chose TLSv1, but that protocol version is not enabled or not supported by the client.". ClientConnectionId:22917695-6f2a-432e-96e8-e777f4237acc

打开 jdk 目录下的 jre\lib\security,打开java.security。把 3DES_EDE_CBC 注释掉。保存。

 

posted @ 2019-07-20 10:42  Helios_Fz  阅读(340)  评论(0编辑  收藏  举报