Tomcat connects to MySQL through socket and no longer occupies the service port [linux]

Source: Internet
Author: User

MySQL Connection Methods

Http://icbm.iteye.com/blog/1840673

In addition to the most common TCP connection mode, MySQL also provides SOCKET (default LINUX connection mode), PIPE, and shared memory connection methods.
The following table lists the server and client startup options and default connection values of each connection method:

TCPLinux ):

Target

Startup options

Default Value

SERVER

-- Port = PORT

3306

CLIENT

-- Protocol = TCP -- port = PORT

3306

SOCKETLinux ):

Target

Startup options

Default Value

SERVER

-- Enable-named-pipe -- socket = SOCKET

/Tmp/mysql. sock

CLIENT

-- Protocol = SOCKET -- socket = SOCKET

/Tmp/mysql. sock

PIPEWindows ):

Target

Startup options

Default Value

SERVER

-- Enable-named-pipe -- socket = SOCKET

MYSQL

CLIENT

-- Protocol = PIPE -- socket = SOCKET

MYSQL

SHARED MEMORYWindows ):

Target

Startup options

Default Value

SERVER

-- Shared-memory-base-name = MEMORY

MySQL

CLIENT

-- Protocol = MEMORY -- shared-memory-base-name = MEMORY

MySQL

The following describes how to use socket in Linux, so that mysql service port 3306 is not needed.

Junixsocket

Https://code.google.com/p/junixsocket

Junixsocket-1.3-bin.tar

Junixsocket-1.3.jar

Junixsocket-mysql-1.3.jar

Tomcat configuration

Because the connection pool of dbcp and c3p0 does not support this, use jdbc connection pool configuration. For more information, see

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 configuration can be used in the automatic scanning package provided by 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>

Difference from 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 package and Runtime Library

Tomcat-jdbc.jar

Tomcat-juli.jar

Tomcat-juli-adapters.jar

Copy lib-native to/opt/newsclub in junixsocket-1.3-bin.tar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.