MariaDB:安裝、配置、JAVA源代碼樣例

      MariaDB的目的是徹底兼容MySQL,包括API和命令行,使之能輕鬆成爲MySQL的代替品。在存儲引擎方面,使用XtraDB來代替MySQL的InnoDB。 java

        MariaDB由MySQL的創始人麥克爾·維德紐斯主導開發,他早前曾以10億美圓的價格,將本身建立的公司MySQL AB賣給了SUN,此後,隨着SUN被甲骨文收購,MySQL的全部權也落入Oracle的手中。MariaDB名稱來自麥克爾·維德紐斯的女兒瑪麗亞(英語:Maria)的名字 node

1.設置yum源 mysql

參考:https://downloads.mariadb.org/mariadb/repositories/ linux

選擇操做系統的類型、版本、MariaDB的版本,會自動生成yum源 sql

# MariaDB 5.5 RedHat repository list - created 2014-02-20 01:40 UTC 數據庫

# http://mariadb.org/mariadb/repositories/ vim

[mariadb] 安全

name = MariaDB socket

baseurl = http://yum.mariadb.org/5.5/rhel6-amd64 ide

gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB

gpgcheck=1

2.安裝mariaDB(Installing MariaDB with YUM)

參考:https://mariadb.com/kb/en/installing-mariadb-with-yum/

yum install MariaDB-server MariaDB-client

yum remove MariaDB-Galera-server

yum install MariaDB-Galera-server MariaDB-client galera

3.啓動

/etc/init.d/mysql start

4.手工導入MariaDB Signing Key

rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB

5.卸載mariadb

rpm -aq|grep -i mariadb|xargs rpm -e --nodeps

若是報:Running Transaction

Error in PREUN scriptlet in rpm package MariaDB-server

MariaDB-server-5.5.35-1.x86_64 was supposed to be removed but is not!

須要運行以下命令:

rpm -u --noscripts MariaDB-server-5.5.35-1.x86_64 

rm -rf /var/lib/mysql*

rm -rf /usr/share/mysql*

rm -rf /etc/rc.d/init.d/mysql

rm -rf /etc/my.cnf

6.修改root初始密碼

mysqladmin -u root password 'root'

7.刪除空用戶,加強安全

delete from mysql.user where user='';

刷新權限表,以即可以使更改當即生效。

flush privileges;

8.遠程登陸配置

grant all privileges  on *.* to root@'%' identified by 'root' ;

flush privileges;


9.修改數據庫目錄

因爲MySQL數據庫目錄佔用磁盤比較大,而MySQL默認的數據文件存儲目錄爲/"var/lib/mysql",因此咱們要把目錄移到"/data"根目錄下的"mysqldata"目錄中

cd /data

mkdir  mysqldata

把MySQL服務進程停掉

service mysql stop

mysqladmin -u root -p shutdown


複製數據庫目錄

把MySQL的數據文件移動到了"/data/mysqldata/mysql"下

mv /var/lib/mysql /data/mysqldata

修改my.conf文件

若是"/etc/"目錄下沒有my.cnf配置文件,請到"/usr/share/mysql/"下找到*.cnf文件,拷貝其中一個合適的配置文件到"/etc/"並更名爲"my.cnf"中

cp /usr/share/mysql/my-default.cnf /etc/my.cnf

修改後的內容以下:

[client]

port            = 3306

socket = /data/mysqldata/mysql/mysql.sock

default-character-set=utf8


[mysqld]

port            = 3306

socket  = /data/mysqldata/mysql/mysql.sock

datadir = /data/mysqldata/mysql

character-set-server=utf8

lower_case_table_names=1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

vim /etc/rc.d/init.d/mysql

datadir=/data/mysqldata/mysql


啓動MYSQL(若是不能啓動,打開/etc/selinux/config,把SELINUX=enforcing改成SELINUX=disabled後存盤退出重啓機器試試,必需要重啓)

service mysql start

注意:aria存儲引擎的一些配置,如aria_pagecache_buffer_size

      相關的參數能夠經過命令查到:show variables like '%aria%'

10. JAVA源代碼樣例

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.Timestamp;

public class TestTime {
	public static void main(String[] args) throws Exception {

		Class.forName("org.mariadb.jdbc.Driver");

		Connection conn = DriverManager
				.getConnection(
						"jdbc:mariadb://xxx.xxx.xxx.xxx:3306/test?useUnicode=true&characterEncoding=GBK",
						"root", "root");

		// 數據插入測試
		String sql = "INSERT INTO test (id,name,dCreateTime) VALUES (?,?,?)";
		PreparedStatement stmt = conn.prepareStatement(sql);
		long iDate = System.currentTimeMillis();
		stmt.setInt(1, 3);
		stmt.setString(2, "test data insert");
		stmt.setTimestamp(3, new Timestamp(iDate));
		stmt.executeUpdate();

		// 數據查詢
		Statement stmt2 = conn.createStatement();
		ResultSet rs = stmt2.executeQuery("select * from test");
		ResultSetMetaData meta = rs.getMetaData();
		int columnCount = meta.getColumnCount();
		while (rs.next()) {
			for (int i = 1; i <= columnCount; i++) {
				String colName = meta.getColumnLabel(i);
				System.out.println(colName + ":" + rs.getObject(i));
			}
		}
		
		//數據刪除
		int result=stmt2.executeUpdate("delete from test");
		System.out.println(result);
		
		//數據統計
		rs=stmt2.executeQuery("select count(*) as cn from test");
		rs.next();
		System.out.println(rs.getObject("cn"));

	}
}
相關文章
相關標籤/搜索