MariaDB数据库管理系统是MySQL的一个分支,MariaDB跟MySQL在绝大多数方面是兼容的,对于开发者来说,几乎感觉不到任何不同。目前MariaDB是发展最快的MySQL分支版本,新版本发布速度已经超过了Oracle官方的MySQL版本。那么在Centos等Linux系统中除了安装Mysql,还可以安装MariaDB数据库了。
安装过程
1)在/etc/yum.repos.d/目录创建MariaDB.repo文件,内容如下:
# MariaDB 5.5 CentOS repository list - created 2014-03-04 11:20 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
2)在Shell环境下执行命令
# yum install MariaDB-server MariaDB-client
Loaded plugins: fastestmirror, security
base | 3.7 kB 00:00
base/primary_db | 4.4 MB 01:11
extras | 3.4 kB 00:00
extras/primary_db | 19 kB 00:00
mariadb | 1.9 kB 00:00
mariadb/primary_db | 15 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 2.1 MB 00:22
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package MariaDB-client.x86_64 0:5.5.36-1.el6 will be obsoleting
--> Processing Dependency: MariaDB-common for package: MariaDB-client-5.5.36-1.el6.x86_64
---> Package MariaDB-server.x86_64 0:5.5.36-1.el6 will be obsoleting
---> Package mysql.x86_64 0:5.1.71-1.el6 will be obsoleted
---> Package mysql-server.x86_64 0:5.1.71-1.el6 will be obsoleted
--> Running transaction check
---> Package MariaDB-common.x86_64 0:5.5.36-1.el6 will be installed
--> Processing Dependency: MariaDB-compat for package: MariaDB-common-5.5.36-1.el6.x86_64
--> Running transaction check
---> Package MariaDB-compat.x86_64 0:5.5.36-1.el6 will be obsoleting
---> Package mysql-libs.x86_64 0:5.1.71-1.el6 will be obsoleted
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================
Package Arch Version Repository Size
==================================================================================================================
Installing:
MariaDB-client x86_64 5.5.36-1.el6 mariadb 10 M
replacing mysql.x86_64 5.1.71-1.el6
MariaDB-compat x86_64 5.5.36-1.el6 mariadb 2.7 M
replacing mysql-libs.x86_64 5.1.71-1.el6
MariaDB-server x86_64 5.5.36-1.el6 mariadb 42 M
replacing mysql-server.x86_64 5.1.71-1.el6
Installing for dependencies:
MariaDB-common x86_64 5.5.36-1.el6 mariadb 23 k
Transaction Summary
==================================================================================================================
Install 4 Package(s)
Total download size: 55 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): MariaDB-5.5.36-centos6-x86_64-client.rpm | 10 MB 04:45
(2/4): MariaDB-5.5.36-centos6-x86_64-common.rpm | 23 kB 00:00
(3/4): MariaDB-5.5.36-centos6-x86_64-compat.rpm | 2.7 MB 01:02
(4/4): MariaDB-5.5.36-centos6-x86_64-server.rpm | 42 MB 17:44
------------------------------------------------------------------------------------------------------------------
Total 40 kB/s | 55 MB 23:34
warning: rpmts_HdrFromFdno: Header V4 DSA/SHA1 Signature, key ID 1bb943db: NOKEY
Retrieving key from https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
Importing GPG key 0x1BB943DB:
Userid: "Daniel Bartholomew (Monty Program signing key) <dbart@askmonty.org>"
From : https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : MariaDB-compat-5.5.36-1.el6.x86_64 1/7
Installing : MariaDB-common-5.5.36-1.el6.x86_64 2/7
Error in PREIN scriptlet in rpm package MariaDB-server-5.5.36-1.el6.x86_64
******************************************************************
A MySQL or MariaDB server package (mysql-server-5.1.71-1.el6.x86_64) is installed.
The current MariaDB server package is provided by a different
vendor (CentOS) than Monty Program AB. Some files may be installed
to different locations, including log files and the service
startup script in /etc/init.d/.
Upgrading directly from MySQL 5.1 to MariaDB 5.5 may not
be safe in all cases. A manual dump and restore using mysqldump is
recommended. It is important to review the MariaDB manual's Upgrading
section for version-specific incompatibilities.
A manual upgrade is required.
- Ensure that you have a complete, working backup of your data and my.cnf
files
- Shut down the MySQL server cleanly
- Remove the existing MySQL packages. Usually this command will
list the packages you should remove:
rpm -qa | grep -i '^mysql-'
You may choose to use 'rpm --nodeps -ev <package-name>' to remove
the package which contains the mysqlclient shared library. The
library will be reinstalled by the MariaDB-shared package.
- Install the new MariaDB packages supplied by Monty Program AB
- Ensure that the MariaDB server is started
- Run the 'mysql_upgrade' program
This is a brief description of the upgrade process. Important details
can be found in the MariaDB manual, in the Upgrading section.
******************************************************************
error: %pre(MariaDB-server-5.5.36-1.el6.x86_64) scriptlet failed, exit status 1
error: install: %pre scriptlet failed (2), skipping MariaDB-server-5.5.36-1.el6
Installing : MariaDB-client-5.5.36-1.el6.x86_64 4/7
Erasing : mysql-5.1.71-1.el6.x86_64 5/7
Erasing : mysql-libs-5.1.71-1.el6.x86_64 6/7
Verifying : MariaDB-common-5.5.36-1.el6.x86_64 1/7
Verifying : MariaDB-compat-5.5.36-1.el6.x86_64 2/7
Verifying : MariaDB-client-5.5.36-1.el6.x86_64 3/7
Verifying : mysql-libs-5.1.71-1.el6.x86_64 4/7
Verifying : MariaDB-server-5.5.36-1.el6.x86_64 5/7
mysql-server-5.1.71-1.el6.x86_64 was supposed to be removed but is not!
Verifying : mysql-server-5.1.71-1.el6.x86_64 6/7
Verifying : mysql-5.1.71-1.el6.x86_64 7/7
Installed:
MariaDB-client.x86_64 0:5.5.36-1.el6 MariaDB-compat.x86_64 0:5.5.36-1.el6
Dependency Installed:
MariaDB-common.x86_64 0:5.5.36-1.el6
Replaced:
mysql.x86_64 0:5.1.71-1.el6 mysql-libs.x86_64 0:5.1.71-1.el6
Failed:
MariaDB-server.x86_64 0:5.5.36-1.el6 mysql-server.x86_64 0:5.1.71-1.el6
Complete!
3、启动MariaDB
# /etc/init.d/mysqld start
Initializing MySQL database: WARNING: The host 'Cloud-Q2' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h Cloud-Q2 password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
[ OK ]
Starting mysqld: [ OK ]
4、为MariaDB设置密码
# /usr/bin/mysqladmin -u root password 'adminadmin'
登陆MariaDB
MySQL [(none)]> use mysql;
MySQL [mysql]> update user set host='%' where user='root' and host='127.0.0.1';
MySQL [mysql]> grant all privileges on *.* to 'root'@'%' identified by 'adminadmin' with grant option;
MySQL [mysql]> flush privileges;
5、让防火墙通过3306端口
# vi /etc/sysconfig/iptables
添加如下内容:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
保存并退出编辑
重启防火墙服务
# service iptables restart
Centos 使用YUM安装MariaDB
远程连接检查,一切OK!centos安装mariadb
说明: 首先必须能链接外网. 如果不能直接访问,那也可以设置代理,请参考: 在内网机器上设置yum代理
使用 yum 的权限要求是 root 用户,如果你不是,那么可以需要 在 shell命令之前加上 sudo, 或者 su root 切换到 super 管理员进行操作. 并可能需要输入密码.
1. 添加 yum 数据源;
建议命名为 MariaDB.repo 类似的名字:
cd /etc/yum.repos.d/
vim /etc/yum.repos.d/MariaDB.repo
然后,写入文件内容:
# /etc/yum.repos.d/MariaDB.repo
# MariaDB 5.5 CentOS repository list - created 2013-12-23 10:27 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
该文件的内容是参考官网,并从官网上生成的,设置安装源仓库的 具体的地址为: https://downloads.mariadb.org/mariadb/repositories/
选择好操作系统版本之后既可以查看,其他操作系统的安装源也可以在此处查看并设置。
如果服务器不支持https协议,或者gpgkey 保错,确保没问题的话,可以将 gpgcheck=1 修改为 gpgcheck=0,则不进行校验.
2. 安装数据库
# yum remove MariaDB-server MariaDB-client
yum -y install MariaDB-server MariaDB-client
如果要删除旧的数据库可以使用remove, 参数 -y 是确认,不用提示。此处,安装的是服务器和客户端,一般来说安装这两个就可以了。
3. 启动数据库
如果不用进行其他的操作,则现在就可以直接启动数据库,并进行测试了。
# 查看mysql状态;关闭数据库
# service mysql status
# service mysql stop
# 启动数据库
service mysql start
4. 修改root密码
# 修改root密码
mysqladmin -u root password 'root'
因为安装好以后的root密码是空,所以需要设置; 如果是测试服务器,那么你可以直接使用root,不重要的密码很多时候可以设置为和用户名一致,以免忘记了又想不起来。
如果是重要的服务器,请使用复杂密码,例如邮箱,各种自由组合的规则的字符等。
5. 登录数据库
mysql -u root -p
如果是本机,那可以直接使用上面的命令登录,当然,需要输入密码. 如果是其他机器,那么可能需要如下的形式:
mysql -h 127.0.0.1 -P 3306 -u root -p
6. 简单SQL测试
>
-- 查看MySQL的状态
status;
-- 显示支持的引擎
show engines;
-- 显示所有数据库
show databases;
-- 切换数据库上下文,即设置当前会话的默认数据库
use test;
-- 显示本数据库所有的表
show tables;
-- 创建一个表
CREATE TABLE t_test (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
userId char(36),
lastLoginTime timestamp,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 插入测试数据
insert into t_test(userId)
values
('admin')
,('haha')
;
-- 简单查询
select * from t_test;
select id,userId from t_test where userId='admin' ;
7. 修改数据存放目录
mysql, MariaDB 的默认数据存放在 /var/lib/mysql/ 目录下,如果不想放到此处,或者是想要程序和数据分离,或者是磁盘原因,需要切换到其他路径,则可以通过修改 datadir系统变量来达成目的.
# 停止数据库
service mysql stop
# 创建目录,假设没有的话
mkdir /usr/local/ieternal/mysql_data
# 拷贝默认数据库到新的位置
# -a 命令是将文件属性一起拷贝,否则各种问题
cp -a /var/lib/mysql /usr/local/ieternal/mysql_data
# 备份原来的数据
cp -a /etc/my.cnf /etc/my.cnf_original
# 其实查看 /etc/my.cnf 文件可以发现
# MariaDB 的此文件之中只有一个包含语句
# 所以需要修改的配置文件为 /etc/my.cnf.d/server.cnf
cp /etc/my.cnf.d/server.cnf /etc/my.cnf.d/server.cnf_original
vim /etc/my.cnf.d/server.cnf
然后 按 i 进入编辑模式,可以插入相关内容.使用键盘的上下左右键可以移动光标, 编辑完成以后,按 ESC 退出编辑模式(进入命令模式), 然后输入命令:wq 保存并退出
# 在文件的 mysqld 节下添加内容
[mysqld]
datadir=/usr/local/ieternal/mysql_data/mysql
socket=/var/lib/mysql/mysql.sock
#default-character-set=utf8
character_set_server=utf8
slow_query_log=on
slow_query_log_file=/usr/local/ieternal/mysql_data/slow_query_log.log
long_query_time=2
其中,也只有 datadir 和 socket 比较重要; 而 default-character-set 是 mysql 自己认识的,而 mariadb5.5 就不认识,相当于变成了 character_set_server
7.1 创建慢查询日志文件
既然上面指定了慢查询日志文件,我后来看了下MariaDB的err日志,发现MariaDB不会自己创建该文件,所以我们需要自己创建,并修改相应的文件权限(比如 mysql 采用 mysql用户,可能我们使用 root用户创建的文件,此时要求慢查询日志文件对mysql用户可读可写就行。)
touch /usr/local/ieternal/mysql_data/slow_query_log.log
chmod 666 /usr/local/ieternal/mysql_data/slow_query_log.log
然后重新启动MySQL.
service mysql start