MySQL连接方式的说明
http://icbm.iteye.com/blog/1840673
MySQL除了最常见的TCP连接方式外,还提供SOCKET(LINUX默认连接方式)、PIPE和SHARED MEMORY连接方式。
各连接方式的服务器、客户端启动选项,及连接默认值见下表:
TCP连接(Linux,Windows):
目标 |
启动选项 |
默认值 |
SERVER |
--port=PORT |
3306 |
CLIENT |
--protocol=TCP --port=PORT |
3306 |
SOCKET连接(Linux):
目标 |
启动选项 |
默认值 |
SERVER |
--enable-named-pipe --socket=SOCKET |
/tmp/mysql.sock |
CLIENT |
--protocol=SOCKET --socket=SOCKET |
/tmp/mysql.sock |
PIPE连接(Windows):
目标 |
启动选项 |
默认值 |
SERVER |
--enable-named-pipe --socket=SOCKET |
MYSQL |
CLIENT |
--protocol=PIPE --socket=SOCKET |
MYSQL |
SHARED MEMORY连接(Windows):
目标 |
启动选项 |
默认值 |
SERVER |
--shared-memory --shared-memory-base-name=MEMORY |
MySQL |
CLIENT |
--protocol=MEMORY --shared-memory-base-name=MEMORY |
MySQL |
下面说明一下在Linux平台下使用socket形式的具体步骤,这样就可以不用3306的mysql服务端口了
junixsocket
https://code.google.com/p/junixsocket
junixsocket-1.3-bin.tar
junixsocket-1.3.jar
junixsocket-mysql-1.3.jar
tomcat的配置
由于dbcp,c3p0的连接池不支持这个,因此使用jdbc的连接池配置,具体的参考文档见
https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html
applicationContext.xml
<context:property-placeholder
ignore-unresolvable="true" location="classpath:/application.properties" />
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="defaultCatalog" value="${jdbc.defaultCatalog}" />
<property name="dbProperties">
<props>
<prop key="socketFactory">org.newsclub.net.mysql.AFUNIXDatabaseSocketFactory</prop>
<prop key="junixsocket.file">/tmp/mysql.sock</prop>
</props>
</property>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource">
<!-- mapping的配在可以用spring提供的自动扫描包 -->
<property name="packagesToScan" value="cn.csbit.**.model" />
<property name="hibernateProperties">
<props>
<prop key="javax.persistence.validation.mode">none</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>
和dbcp的区别
application.properties
#mysql database setting
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://?useUnicode=true&characterEncoding=utf-8&useLocalSessionState=true&autoReconnect=true
jdbc.username=***
jdbc.password=***
jdbc.defaultCatalog=***
jar包和运行库
tomcat-jdbc.jar
tomcat-juli.jar
tomcat-juli-adapters.jar
junixsocket-1.3-bin.tar中lib-native拷贝到 /opt/newsclub 下