美文网首页渣男的道
centos7 配置安装Mariadb数据库

centos7 配置安装Mariadb数据库

作者: a564c12b3104 | 来源:发表于2018-11-05 16:46 被阅读0次

centos7 yum配置安装Mariadb数据库(使用国内Mariadb源)

CentOS 6 或早期的版本中提供的是 MySQL 的服务器/客户端安装包,但 CentOS 7 已使用了 MariaDB 替代了默认的 MySQL。MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

Linux下安装MariaDB官方文档参见:mariadb官网地址

全部删除MySQL/MariaDB

# 搜索 MariaDB 现有的包
rpm -qa | grep MariaDB

# 如果存在(删不了的话,就一个一个删),删除
rpm -e --nodeps MariaDB-*

# 搜索 mysql 现有的包:
rpm -qa | grep mysql

# 如果存在(删除你实际查询出来的mysql结果即可),删除
yum remove mysql mysql-server mysql-libs compat-mysql51

开始安装 MariaDB

# 创建 MariaDB 的yum 源
vim /etc/yum.repos.d/MariaDB.repo

# 插入一下内容,第二个换成了国内源中科大的(有问题可查看, http://mirrors.ustc.edu.cn/help/mariadb.html ),(系统及版本选择 https://downloads.mariadb.org/mariadb/repositories/#mirror=neusoft ,选择安装你需要的版本)

# 国外的源
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

# 国内中科大的源
[mariadb]
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.1/centos7-amd64/
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

# 安装Mariadb之前,你可以先导入GPG key
rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB

# 运行安装命令安装MariaDB(安装过程可能会比较漫长,使用中科大的就快了)
yum -y install MariaDB-server MariaDB-client

# 查看是否安装成功
yum list installed maria*

# 安装成功之后启动MariaDB服务,并设为开机自启。
systemctl start mariadb #启动服务
systemctl enable mariadb #设置开机启动
systemctl stop mariadb.service #停止MariaDB
systemctl restart mariadb #重新启动

MariaDB初始化设置,先退出数据库

mysql_secure_installation

Enter current password for root (enter for none):<–先输入登录密码,初次运行直接回车

Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码

Remove anonymous users? [Y/n] <– 是否删除匿名用户,回车

Disallow root login remotely? [Y/n] <–是否禁止root远程登录,回车(后面授权配置)

Remove test database and access to it? [Y/n] <– 是否删除test数据库,回车

Reload privilege tables now? [Y/n] <– 是否重新加载权限表,回车

配置MariaDB的字符集

# 使用vim /etc/my.cnf.d/server.cnf命令编辑server.cnf文件,在[mysqld]标签下添加:

init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

# 保存并重启Mariadb
systemctl restart mariadb

# 登录Mariadb后查看字符集
show variables like "%character%";show variables like "%collation%";

结果:

Variable_name Value
character_set_client utf8
character_set_connection utf8
character_set_database utf8
character_set_filesystem binary
character_set_results utf8
character_set_server utf8
character_set_system utf8
character_sets_dir /usr/share/mysql/charsets/
Variable_name Value
collation_connection utf8_unicode_ci
collation_database utf8_unicode_ci
collation_server utf8_unicode_ci

添加用户,设置权限

# 创建用户命令(用户名,密码请自行修改)
create user username@localhost identified by 'password';

# 授予外网登陆权限
grant all privileges on *.* to username@'%' identified by 'password';

授予部分权限只需把all privileges改为select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分即可。

# 查看
show databases;
use mysql;
select host,user,password from user;

结果:

host user password
localhost root *B026E5F4BC95C55D6B633126E619A8F805DDC5C3
127.0.0.1 root *B026E5F4BC95C55D6B633126E619A8F805DDC5C3
::1 root *B026E5F4BC95C55D6B633126E619A8F805DDC5C3
% name *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9
localhost name *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9

现在就配置成功了,注意一下防火墙!!!

相关文章

网友评论

    本文标题:centos7 配置安装Mariadb数据库

    本文链接:https://www.haomeiwen.com/subject/atgsxqtx.html