MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。在存储引擎方面,使用XtraDB来代替MySQL的InnoDB。
MariaDB由MySQL的创始人麦克尔·维德纽斯主导开发,他早前曾以10亿美元的价格,将自己创建的公司MySQL AB卖给了SUN,此后,随着SUN被甲骨文收购,MySQL的所有权也落入Oracle的手中。MariaDB名称来自麦克尔·维德纽斯的女儿玛丽亚(英语:Maria)的名字
1.设置yum源
参考:https://downloads.mariadb.org/mariadb/repositories/
选择操作系统的类型、版本、MariaDB的版本,会自动生成yum源
# MariaDB 5.5 RedHat repository list - created 2014-02-20 01:40 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/rhel6-amd64
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"));
}
}
来源:oschina
链接:https://my.oschina.net/u/1410765/blog/201569