MariaDB_About the MariaDB Java Client

via: https://mariadb.com/kb/en/about-the-mariadb-java-client/

 

Introduction

The MariaDB Client Library for Java Applications is a Type 4 JDBC driver. It was developed specifically as a lightweight JDBC connector for use with MySQL and MariaDB database servers. It's originally based on the Drizzle JDBC code, and with a lot of additions and bug fixes.

Obtaining the driver

The driver (jar and source code) can be downloaded from https://downloads.mariadb.org/client-java/

Installing the driver

Installation is as simple as placing the .jar file in your classpath.

Requirements

  • Java 6
  • A MariaDB or MySQL Server
  • maven (only if you want build from source)

Source code

The source code is available on Launchpad: https://launchpad.net/mariadb-java-client. Development version can be obtained using

bzr branch lp:mariadb-java-client

License

GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

Building and testing the driver

The section deals with building the connector from source and testing it. If you have downloaded a ready built connector, in a jar file, then this section may be skipped.

MariaDB Client Library for Java Applications uses maven for build. You first need to ensure you have both java and maven installed on your server before you can build the driver.

To run the unit test, you'll need a MariaDB or MySQL server running on localhost (on default TCP port 3306) and a database called 'test', and user 'root' with empty password

$ bzr branch lp:mariadb-java-client #  Or, unpack the source distribution tarball
$ cd mariadb-java-client
# For the unit test run, start local mysqld mysqld, 
# ensure that user root with empty password can login
$ mvn package
# If you want to build without running unit  tests, use
# mvn -Dmaven.test.skip=true package

After that , you should have JDBC jar mariadb-java-client-x.y.z.jar in the 'target' subdirectory

Installing the driver

Installation of the client library is very simple, the jar file should be saved in an appropriate place for your application and the classpath of your application altered to include the MariaDB Client Library for Java Applications rather than your current connector.

Using the driver

The following subsections show the formatting of JDBC connection strings for MariaDB, MySQL database servers. Additionally, sample code is provided that demonstrates how to connect to one of these servers and create a table.

Driver Manager

Applications designed to use the driver manager to locate the entry point need no further configuration, the MariaDB Client Library for Java Applications will automatically be loaded and used in the way any previous MySQL driver would have been.

Driver Class

Please note that the driver class provided by the MariaDB Client Library for Java Applications is notcom.mysql.jdbc.Driver but org.mariadb.jdbc.Driver!

Connection strings

Format of the JDBC connection string is

jdbc:mysql://<host>:<port>/<database>?<key1>=<value1>&<key2>=<value2>...

Altenatively

jdbc:mariadb://<host>:<port>/<database>?<key1>=<value1>&<key2>=<value2>...

can also be used.

what't more,for cluster,it should be like this, jdbc:mariadb://<host1>:<port1>,<host2>:<port2>/<database>?<key1>=<value1>&<key2>=<value2>...

Optional URL parameters

General remark: Unknown options accepted and are silently ignored.

Following options are currently supported.

keydescriptionsupported since version
userDatabase user name1.0.0
passwordPassword of database user1.0.0
fastConnectIf set, skips check for sql_mode, assumes NO_BACKSLASH_ESCAPES is *not* set1.0.0
useFractionalSecondsCorrectly handle subsecond precision in timestamps (feature available with MariaDB 5.3 and later).May confuse 3rd party components (Hibernated)1.0.0
allowMultiQueriesAllows multiple statements in single executeQuery1.0.0
dumpQueriesOnExceptionIf set to 'true', exception thrown during query execution contain query string1.1.0
useCompressionallow compression in MySQL Protocol1.0.0
useSSLForce SSL on connection1.1.0
trustServerCertificateWhen using SSL, do not check server's certificate1.1.1
serverSslCertServer's certificatem in DER form, or server's CA certificate. Can be used in one of 3 forms, sslServerCert=/path/to/cert.pem (full path to certificate), sslServerCert=classpath:relative/cert.pem (relative to current classpath), or as verbatim DER-encoded certificate string "------BEGING CERTIFICATE-----"1.1.3
socketFactoryto use custom socket factory, set it to full name of the class that implements javax.net.SocketFactory1.0.0
tcpNoDelaySets corresponding option on the connection socket1.0.0
tcpKeepAliveSets corresponding option on the connection socket1.0.0
tcpAbortiveCloseSets corresponding option on the connection socket1.1.1
tcpRcvBufset buffer size for TCP buffer (SO_RCVBUF)1.0.0
tcpSndBufset buffer size for TCP buffer (SO_SNDBUF)1.0.0
pipeOn Windows, specify named pipe name to connect to mysqld.exe1.1.3
tinyInt1isBitDatatype mapping flag, handle MySQL Tiny as BIT(boolean)1.0.0
yearIsDateTypeYear is date type, rather than numerical1.0.0
sessionVariables<var>=<value> pairs separated by comma, mysql session variables, set upon establishing successfull connection1.1.0
localSocketAllows to connect to database via Unix domain socket, if server allows it. The value is the path of Unix domain socket, i.e "socket" database parameter1.1.4
sharedMemoryAllowed to connect database via shared memory, if server allows it. The value is base name of the shared memory1.1.4

JDBC API Implementation Notes

Streaming result sets

By default, Statement.executeQuery() will read full result set from server before returning. With large result sets, this will require large amounts of memory. Better behavior in this case would be reading row-by-row, with ResultSet.next(), so called "streaming" feature. It is activated using Statement.setFetchSize(Integer.MIN_VALUE)

Prepared statements

The driver only uses text protocol to communicate with the database. Prepared statements (parameter substitution) is handled by the driver, on the client side.

CallableStatement

Callable statement implementation won't need to access stored procedure metadata ( mysql.proc) table, if both of following is true

  • CallableStatement.getMetadata() is not used
  • Parameters are accessed by index, not by name

When possible, following 2 rules above provides both better speed and eliminates concerns about SELECT privileges on mysql.proc table

Optional JDBC classes

Following optional interfaces are implemented by the org.mariadb.jdbc.MySQLDataSource class : javax.sql.DataSource, javax.sql.ConnectionPoolDataSource, javax.sql.XADataSource

Usage examples

The following code provides a basic example of how to connect to a MariaDB or MySQL server and create a table.

Creating a table on a MariaDB or MySQL Server
Connection  connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password");
Statement stmt = connection.createStatement();
stmt.executeUpdate("CREATE TABLE a (id int not null primary key, value varchar(20))");
stmt.close();
connection.close();

 

### MARIADB_MYSQL_LOCALHOST_USER环境变量的作用与意义 MARIADB_MYSQL_LOCALHOST_USER 是一个常见的环境变量,通常用于配置 MariaDB 或 MySQL 数据库服务在本地主机上的用户权限管理。具体来说,该环境变量的主要作用是定义一个默认的用户名称,当数据库服务启动时,会自动为 localhost 的连接分配此用户身份[^1]。 #### 环境变量的功能 - **简化用户配置**:通过设置 MARIADB_MYSQL_LOCALHOST_USER,管理员可以避免在每次启动服务或执行脚本时手动指定用户名。 - **增强安全性**:如果此环境变量被正确配置,可以减少使用 root 用户进行本地连接的风险,从而提升系统的安全性[^3]。 - **自动化集成**:在容器化环境中(如 Docker),此变量常用于自动创建或映射特定用户到数据库实例中,以便快速部署和初始化数据库。 #### 示例代码 以下是一个简单的示例,展示如何在 MariaDB 中利用此环境变量来创建用户并赋予权限: ```sql -- 创建一个新用户并授予其对特定数据库的权限 CREATE USER &#39;localuser&#39;@&#39;localhost&#39; IDENTIFIED BY &#39;password&#39;; GRANT ALL PRIVILEGES ON db_example.* TO &#39;localuser&#39;@&#39;localhost&#39;; ``` 上述代码片段展示了如何为 localhost 创建一个名为 `localuser` 的用户,并赋予其对数据库 `db_example` 的所有权限[^4]。 #### 注意事项 - 如果未正确设置 MARIADB_MYSQL_LOCALHOST_USER,可能会导致连接失败或权限不足的问题。例如,出现类似 `Access denied for user &#39;root&#39;@&#39;localhost&#39;` 的错误信息[^3]。 - 在生产环境中,建议避免使用默认用户或过于简单的密码组合,以防止潜在的安全隐患。 ### 总结 MARIADB_MYSQL_LOCALHOST_USER 环境变量的核心意义在于优化本地用户的管理流程,同时提高数据库服务的安全性和易用性。通过合理配置此变量,可以显著简化开发和运维工作中的用户授权操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值