mysql的数据连接池怎么配置文件

mysql的数据连接池怎么配置文件,第1张

mysql的数据连接池怎么配置文件

连接先建立一些连接,并且这些连接允许共享,因此这样就节省了每次连接的时间开销。Mysql数据库为例,连接池在Tomcat中的配置与使用。

1、创建数据库Student,表student

2、配置server.xml文件。Tomcat安装目录下conf中server.xml文件。

<GlobalNamingResources>

<Resource

name="jdbc/DBPool"

type="javax.sql.DataSource"

password=""

driverClassName="com.mysql.jdbc.Driver"

maxIdle="2"

maxWait="5000"

username="root"

url="jdbc:mysql://localhost:3306/student"

maxActive="3"

/>

</GlobalNamingResources>

name:指定连接池的名称

type:指定连接池的类,他负责连接池的事务处理

url:指定要连接的数据库

driverClassName:指定连接数据库使用的驱动程序

username:数据库用户名

password:数据库密码

maxWait:指定最大建立连接等待时间,如果超过此时间将接到异常

maxIdle:指定连接池中连接的最大空闲数

maxActive:指定连接池最大连接数

3、配置web.xml文件。

<web-app>

<resource-ref>

<description>mysql数据库连接池配置</description>

<res-ref-name>jdbc/DBPool</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

<res-sharing-scope>Shareable</res-sharing-scope>

</resource-ref>

</web-app>

4、配置context.xml文件

与server.xml文件所在的位置相同。

<Context>

<ResourceLink

name="jdbc/DBPool"

type="javax.sql.DataSource"

global="jdbc/DBPool"

/>

</Context>

5、测试

DataSource pool = null

Context env = null

Connection conn = null

Statement st = null

ResultSet rs = null

try{

env = (Context)new InitialContext().lookup("java:comp/env")

//检索指定的对象,返回此上下文的一个新实例

pool = (DataSource)env.lookup("jdbc/DBPool")

//获得数据库连接池

if(pool==null){out.printl("找不到指定的连接池!")}

con = pool.getConnection()

st = con.createStatement()

rs = st.executeQuery("select * from student")

}catch(Exception ex){out.printl(ne.toString())}

查找my.cnf文件路径;

从上可以看出, 服务器首先会读取/etc/my.cnf文件,如果发现该文件不存在,再依次尝试从后面的几个路径进行读取。

参数详解:

[client] #客户端设置,即客户端默认的连接参数

port = 3307 #默认连接端口

socket = /data/mysqldata/3307/mysql.sock #用于本地连接的socket套接字

default-character-set = utf8mb4 #编码

[mysqld] #服务端基本设置

port = 3307 MySQL监听端口

socket = /data/mysqldata/3307/mysql.sock #为MySQL客户端程序和服务器之间的本地通讯指定一个套接字文件

pid-file = /data/mysqldata/3307/mysql.pid#pid文件所在目录

basedir = /usr/local/mysql-5.7.11#使用该目录作为根目录(安装目录)

datadir = /data/mysqldata/3307/data #数据文件存放的目录

tmpdir = /data/mysqldata/3307/tmp #MySQL存放临时文件的目录

character_set_server = utf8mb4 #服务端默认编码(数据库级别)

collation_server = utf8mb4_bin #服务端默认的比对规则,排序规则

user = mysql #MySQL启动用户

log_bin_trust_function_creators = 1 #This variable applies when binary logging is enabled. It controls whether stored function creators can be trusted not to create stored functions that will cause #unsafe events to be written to the binary log. If set to 0 (the default), users are not permitted to create or alter stored functions unless they have the SUPER #privilege in addition to the CREATE ROUTINE or ALTER ROUTINE privilege. 开启了binlog后,必须设置这个值为1.主要是考虑binlog安全

performance_schema = 0 #性能优化的引擎,默认关闭

secure_auth = 1 #secure_auth 为了防止低版本的MySQL客户端(


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11971713.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存