文章詳情頁(yè)
MySql連接數(shù)據(jù)庫(kù)常用參數(shù)及代碼解讀
瀏覽:335日期:2023-02-18 16:43:59
目錄
- MySql連接數(shù)據(jù)庫(kù)常用參數(shù)及代碼
- 常用參數(shù)
- 數(shù)據(jù)庫(kù)工具類
- MySql常用參數(shù)
- 總結(jié)
MySql連接數(shù)據(jù)庫(kù)常用參數(shù)及代碼
常用參數(shù)
數(shù)據(jù)庫(kù)工具類
package util;
import java.sql.Connection;
import java.sql.DriverManager;
/**
?* 數(shù)據(jù)庫(kù)工具類
?* @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";
?? ?
?? ?/**
?? ? * 獲取數(shù)據(jù)庫(kù)連接
?? ? * @return
?? ? * @throws Exception
?? ? */
?? ?public Connection getCon() throws Exception{
?? ??? ?Class.forName(jdbcName);
?? ??? ?Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
?? ??? ?return con;
?? ?}
?? ?
?? ?/**
?? ? * 關(guān)閉數(shù)據(jù)庫(kù)連接
?? ? * @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("數(shù)據(jù)庫(kù)連接成功");
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
}
調(diào)用該工具類時(shí)
?? ?Connection con=null;
?? ?try{
?? ??? ??? ?con=dbUtil.getCon();
?? ??? ??? ?(數(shù)據(jù)庫(kù)操作語(yǔ)句)
?? ??? ?}catch(Exception e){
?? ??? ??? ?e.printStackTrace();
?? ??? ?}finally{
?? ??? ??? ?try {
?? ??? ??? ??? ?dbUtil.closeCon(con);
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
MySql常用參數(shù)
參數(shù)名稱參數(shù)說(shuō)明參考max_connections數(shù)據(jù)庫(kù)的最大連接數(shù)MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variableswait_timeout非交互模式的沒(méi)有操作后的超時(shí)秒數(shù),超時(shí)后數(shù)據(jù)庫(kù)會(huì)關(guān)閉該連接。MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variablesinteractive_timeout交互模式下的沒(méi)有操作后的超時(shí)時(shí)間,一般與wait_timeout的值一致MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variablesmax_prepared_stmt_count限制一個(gè)session內(nèi)最多可以有多少條預(yù)編譯語(yǔ)句MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variablesinnodb_lock_wait_timeout等待數(shù)據(jù)鎖的超時(shí)時(shí)間默認(rèn)50s,超時(shí)則回滾當(dāng)前事務(wù),可以設(shè)置適當(dāng)?shù)臅r(shí)長(zhǎng)作為避免死鎖的一種措施MySQL :: MySQL 8.0 Reference Manual :: 15.14 InnoDB Startup Options and System Variablesmax_allowed_packet服務(wù)器和客戶之間單次通信的大小上限,會(huì)限制Server接受的數(shù)據(jù)包大小。MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variablesslow_query_log是否開啟慢日志 0關(guān)閉, 1開啟MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variableslong_query_time如果開啟了慢日志,則sql查詢時(shí)間超過(guò)該值就記錄到慢日志MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variablesinnodb_flush_log_at_trx_commit控制提交操作的嚴(yán)格ACID合規(guī)性與在批量重新安排和完成提交相關(guān)I/O操作時(shí)可能實(shí)現(xiàn)的更高性能之間的平衡。完全符合ACID要求需要默認(rèn)設(shè)置1。日志在每次事務(wù)提交時(shí)寫入并刷新到磁盤。
設(shè)置為0時(shí),日志每秒寫入并刷新到磁盤一次。未刷新日志的事務(wù)可能會(huì)在崩潰中丟失。
設(shè)置為2時(shí),日志在每次事務(wù)提交后寫入,并每秒刷新一次到磁盤。未刷新日志的事務(wù)可能會(huì)在崩潰中丟失。MySQL :: MySQL 8.0 Reference Manual :: 15.14 InnoDB Startup Options and System Variablessync_binlog控制MySQL服務(wù)器將二進(jìn)制日志同步到磁盤的頻率。
sync_binlog=0:禁用MySQL服務(wù)器將二進(jìn)制日志同步到磁盤。
sync_binlog=1:?jiǎn)⒂迷谔峤皇聞?wù)之前將二進(jìn)制日志同步到磁盤。
sync_binlog=其他數(shù)字,表示在收集N個(gè)二進(jìn)制日志提交組后,二進(jìn)制日志將同步到磁盤。MySQL :: MySQL 8.0 Reference Manual :: 17.1.6.4 Binary Logging Options and Variablesinnodb_buffer_pool_instancesInnoDB緩沖池劃分的區(qū)域數(shù)。僅在緩沖區(qū)大于1G時(shí)生效,默認(rèn)為8MySQL :: MySQL 8.0 Reference Manual :: 15.14 InnoDB Startup Options and System Variablesinnodb_buffer_pool_chunk_size緩沖池的塊大小(字節(jié))默認(rèn)128MMySQL :: MySQL 8.0 Reference Manual :: 15.14 InnoDB Startup Options and System Variablesinnodb_buffer_pool_size緩沖池的大小(字節(jié)),InnoDB緩存表和索引數(shù)據(jù)的內(nèi)存區(qū)域。緩沖池大小必須始終等于或是innodb_Buffer_pool_chunk_size*innodb_buffer_pool_instances的倍數(shù)MySQL :: MySQL 8.0 Reference Manual :: 15.14 InnoDB Startup Options and System Variables
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持。
標(biāo)簽:
MySQL
相關(guān)文章:
排行榜

網(wǎng)公網(wǎng)安備