新奥资料免费精准期期准,2024新澳天天资料免费大全,澳门六开彩天天正版免费,澳门天天六开彩正版澳门,最准一肖一码一一中特

您的位置:首页技术文章

MySql连接数据库常用参数及代码解读

浏览:15日期:2023-02-05 11:02:01
目录
  • MySql连接数据库常用参数及代码
    • 常用参数
    • 数据库工具类
  • MySql常用参数
    • 总结

      MySql连接数据库常用参数及代码

      常用参数

      参数名称参数说明缺省值最低版本要求user数据库用户名(用于连接数据库)password用户密码(用于连接数据库)useUnicode是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为truefalse1.1gcharacterEncoding当useUnicode设置为true时,指定字符编码。比如可设置为gb2312或gbkfalse1.1gautoReconnect当数据库连接异常中断时,是否自动重新连接?false1.1autoReconnectForPools是否使用针对数据库连接池的重连策略false3.1.3failOverReadOnly自动重连成功后,连接是否设置为只读?true3.0.12maxReconnectsautoReconnect设置为true时,重试连接的次数31.1initialTimeoutautoReconnect设置为true时,两次重连之间的时间间隔,单位:秒21.1connectTimeout和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK 1.4及更高版本03.0.1socketTimeoutsocket操作(读写)超时,单位:毫秒。 0表示永不超时03.0.1allowMultiQueriesmysql驱动开启批量执行sql的开关false

      数据库工具类

      package util;
      
      import java.sql.Connection;
      import java.sql.DriverManager;
      
      /**
      ?* 数据库工具类
      ?* @author Administrator
      ?*
      ?*/
      public class DbUtil {
      
      ?? ?private String dbUrl="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true";
      ?? ?private String dbUserName="root";
      ?? ?private String dbPassword="passwd";
      ?? ?private String jdbcName="com.mysql.jdbc.Driver";
      ?? ?
      ?? ?/**
      ?? ? * 获取数据库连接
      ?? ? * @return
      ?? ? * @throws Exception
      ?? ? */
      ?? ?public Connection getCon() throws Exception{
      ?? ??? ?Class.forName(jdbcName);
      ?? ??? ?Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
      ?? ??? ?return con;
      ?? ?}
      ?? ?
      ?? ?/**
      ?? ? * 关闭数据库连接
      ?? ? * @param con
      ?? ? * @throws Exception
      ?? ? */
      ?? ?public void closeCon(Connection con) throws Exception{
      ?? ??? ?if(con!=null){
      ?? ??? ??? ?con.close();
      ?? ??? ?}
      ?? ?}
      ?? ?
      ?? ?public static void main(String[] args) {
      ?? ??? ?DbUtil dbUtil=new DbUtil();
      ?? ??? ?try {
      ?? ??? ??? ?dbUtil.getCon();
      ?? ??? ??? ?System.out.println("数据库连接成功");
      ?? ??? ?} catch (Exception e) {
      ?? ??? ??? ?// TODO Auto-generated catch block
      ?? ??? ??? ?e.printStackTrace();
      ?? ??? ?}
      ?? ?}
      }

      调用该工具类时

      ?? ?Connection con=null;
      ?? ?try{
      ?? ??? ??? ?con=dbUtil.getCon();
      ?? ??? ??? ?(数据库操作语句)
      ?? ??? ?}catch(Exception e){
      ?? ??? ??? ?e.printStackTrace();
      ?? ??? ?}finally{
      ?? ??? ??? ?try {
      ?? ??? ??? ??? ?dbUtil.closeCon(con);
      ?? ??? ??? ?} catch (Exception e) {
      ?? ??? ??? ??? ?// TODO Auto-generated catch block
      ?? ??? ??? ??? ?e.printStackTrace();
      ?? ??? ??? ?}

      MySql常用参数

      参数名称参数说明参考max_connections数据库的最大连接数MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variableswait_timeout非交互模式的没有操作后的超时秒数,超时后数据库会关闭该连接。MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variablesinteractive_timeout交互模式下的没有操作后的超时时间,一般与wait_timeout的值一致MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variablesmax_prepared_stmt_count限制一个session内最多可以有多少条预编译语句MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variablesinnodb_lock_wait_timeout等待数据锁的超时时间默认50s,超时则回滚当前事务,可以设置适当的时长作为避免死锁的一种措施MySQL :: MySQL 8.0 Reference Manual :: 15.14 InnoDB Startup Options and System Variablesmax_allowed_packet服务器和客户之间单次通信的大小上限,会限制Server接受的数据包大小。MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variablesslow_query_log是否开启慢日志 0关闭, 1开启MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variableslong_query_time如果开启了慢日志,则sql查询时间超过该值就记录到慢日志MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variablesinnodb_flush_log_at_trx_commit控制提交操作的严格ACID合规性与在批量重新安排和完成提交相关I/O操作时可能实现的更高性能之间的平衡。
      完全符合ACID要求需要默认设置1。日志在每次事务提交时写入并刷新到磁盘。
      设置为0时,日志每秒写入并刷新到磁盘一次。未刷新日志的事务可能会在崩溃中丢失。
      设置为2时,日志在每次事务提交后写入,并每秒刷新一次到磁盘。未刷新日志的事务可能会在崩溃中丢失。MySQL :: MySQL 8.0 Reference Manual :: 15.14 InnoDB Startup Options and System Variablessync_binlog控制MySQL服务器将二进制日志同步到磁盘的频率。
      sync_binlog=0:禁用MySQL服务器将二进制日志同步到磁盘。
      sync_binlog=1:启用在提交事务之前将二进制日志同步到磁盘。
      sync_binlog=其他数字,表示在收集N个二进制日志提交组后,二进制日志将同步到磁盘。MySQL :: MySQL 8.0 Reference Manual :: 17.1.6.4 Binary Logging Options and Variablesinnodb_buffer_pool_instancesInnoDB缓冲池划分的区域数。仅在缓冲区大于1G时生效,默认为8MySQL :: MySQL 8.0 Reference Manual :: 15.14 InnoDB Startup Options and System Variablesinnodb_buffer_pool_chunk_size缓冲池的块大小(字节)默认128MMySQL :: MySQL 8.0 Reference Manual :: 15.14 InnoDB Startup Options and System Variablesinnodb_buffer_pool_size缓冲池的大小(字节),InnoDB缓存表和索引数据的内存区域。缓冲池大小必须始终等于或是innodb_Buffer_pool_chunk_size*innodb_buffer_pool_instances的倍数MySQL :: MySQL 8.0 Reference Manual :: 15.14 InnoDB Startup Options and System Variables

      总结

      以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

      标签: MySQL