centos7 安装mariadb

1. 为什么安装mariadb

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。 MariaDB目的是完全兼容MySQL,包括API和命令行,她们有相同的命令、界面,以及在MySQL中的库与API,所以MariaDB可以说是为替换MySQL量身定做的,换用后连数据库都不必转换!并可以获得MariaDB提供的许多更好的新特性

MySQL的所有权在Oracle的手中,在Oracle控制下的MySQL开发,有两个主要问题:

  • MySQL核心开发团队是封闭的,完全没有Oracle之外的成员参加。很多高手即使有心做贡献,也没办法做到。
  • MySQL新版本的发布速度,在Oracle收购Sun之后大为减缓。

最近Oracle进一步闭源的举措更是让人难以安心,众多互联网公司纷纷开始寻求MySQL的替代方案。Apple,Google、Facebook、Twitter、Wiki也大量使用MariaDB,在美国已经掀起了热潮。关于maria和MySQL的对比可参见:数据库对比:选择MariaDB还是MySQL?_功能

从经济适用性角度讲,Mariadb成为当下关系型数据的一种选择,centos也默认支持mariadb作为自己的存储实例。

2. centos 7 安装mariadb

通过命令查看系统是否安装了mariadb:

rpm -qa | grep mariadb

如果存在mariadb 可以选择复用或者卸载,卸载命令为:

rpm -e --nodeps mariadb-5.5.68-1.el7.x86_64
rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64

通过yum源安装mariadb:

yum -y install mariadb-server mariadb-client

配置my.cnf。默认可以从/usr/share/mysql拷贝到/etc/my.cnf。

具体配置如下:

[client]
#password    = your_password
port        = 3306
socket        = /var/lib/mysql/mysql.sock
default-character-set=utf8mb4

# The MySQL server
[mysqld]
port        = 3306
socket        = /var/lib/mysql/mysql.sock
skip-external-locking
# set character
init_connect='SET collation_connection = utf8mb4_unicode_ci'
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
skip-character-set-client-handshake

key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 240K
thread_cache_size = 8
query_cache_size = 32M
myisam_sort_buffer_size = 8M

skip-name-resolve

#skip-networking
server-id    = 1

# Uncomment the following if you want to log updates
log-bin=mysql-bin

# binary logging format - mixed recommended
binlog_format=mixed
expire_logs_days = 10
default_storage_engine = InnoDB
innodb_file_per_table = 1

#binlog_direct_non_transactional_updates=TRUE

# Uncomment the following if you are using InnoDB tables
innodb_data_home_dir = /var/lib/mysql
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /var/lib/mysql

innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M

innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M

[mysqlhotcopy]
interactive-timeout
启动服务:systemctl start mariadb.service

停止服务:systemctl stop mariadb.service

查看服务状态:systemctl status mariadb.service

开机启动:systemctl enable mariadb

启动过程中,如果有错误可以根据日志排查:

3. 配置数据库的用户和远程登录

进入mariadb server设置:

use mysql;

-- 查询当前用户表的数据;

select user, password, host from user;

-- 清除用户名为空的记录

delete from user where user = '';

-- 将与主机名相等的字段改为 "%",当用户从客户端请求登陆时,MySQL将授权表中的条目与客户端所提供的条目进行比较,包括用户的用户名,密码和主机

update user set host='%' where host='virtual-1';

-- 设置root用户的密码

update user set password=password('123') where user='root' ;

flush privileges;

4. 配置服务端防火墙规则

查看服务器防火墙状态:sudo systemctl status firewalld

直接关闭防火墙:systemctl stop firewalld

但是在生产环境,防火墙一般都是打开的,所以需要在防火墙开通对3306端口的放开。

# 开启3306端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent  
# 重启防火墙
firewall-cmd --reload 
# 查看3306端口是否开启
firewall-cmd --query-port=3306/tcp