在Linux上用rpm 安装及配置MariaDB

安装MariaDB

1.切换到root用户,首先执行rpm -qa | grep -i mysql检查一下是否有已安装的与MySQL相关的东西,如果有,使用rpm -e --nodeps mysql*进行强制卸载

2.使用yum安装MariaDB,执行yum -y install mariadb mariadb-server

3.安装完成后,执行systemctl start mariadb 启动MariaDB,执行systemctl enable mariadb设置开机启动

配置MariaDB

1.执行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] <– 是否重新加载权限表,回车

2.配置完成后,执行mysql -uroot -ppassword测试登录。其中root为要登录的用户名,password为刚才设置的root用户的密码

3.测试成功后,配置MariaDB的字符集

- 使用vi编辑器打开/etc/my.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
  • 使用vi编辑器打开/etc/my.cnf.d/client.cnf,在[client]中添加

    default-character-set=utf8

  • 使用vi编辑器打开/etc/my.cnf.d/mysql-clients.cnf,在[mysql]中添加

    default-character-set=utf8

  • 全部保存后,进入到MariaDB控制台,查看字符集

    show variables like "%character%";show variables like "%collation%";

    全部显示UTF-8则配置成功

配置MariaDB远程连接

进入到MariaDB控制台

1.执行如下语句建立用户并赋予所有操作权限。

CREATE USER 'username'@'host' IDENTIFIED BY 'password';

参数说明
username将要创建的用户名
host指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
password该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器

2.给用户赋予远程登录权限

GRANT privileges ON databasename.tablename TO 'username'@'host'

参数说明
privileges用户的操作权限,如SELECT , INSERT , UPDATE 等(权限列表见文末)。如果要授予所的权限则使用ALL
databasename数据库名
tablename表名,如果要授予该用户对所有数据库和表的相应操作权限则可用表示,如.*

3.修改完成后在MariaDB控制台执行FLUSH PRIVILEGES刷新配置权限使其生效

此时即可通过ip远程访问主机上的MariaDB了。

若仍不能访问,可进行以下检查:

1.查看/etc/my.cnf,如skip-networking、bind-address(或bindaddress)被配置,则需要将这两个参数注释掉。

skip-networking 这个参数,会导致所有TCP/IP端口没有被监听,也就是说除了本机,其他客户端都无法用网络连接到本MariaDB服务器。

而bind-address这个参数是指定哪些ip地址被配置,使得MariaDB服务器只回应哪些ip地址的请求

2.如果仍然不能访问,则有可能是防火墙的原因。在shell下执行/etc/init.d/iptables stop关闭防火墙。

附:MariaDB操作权限

权限描述
ALTERAllows use of ALTER TABLE
ALTER ROUTINEAlters or drops stored routines
CREATEAllows user of CREATE TABLE
CREATE ROUTINECreates stored routines
CREATE TEMPORARY TABLEAllows user of CREATE TEMPORARY TABLE
CREATE USERAllows use ofCREATE USER,DROP USER,RENAME USER, and REVOKE ALL PRIVILEGES
CREATE VIEWAllows use of CREATE VIEW
DELETEAllows use of DELETE
DROPAllows use of DROP TABLE
EXECUTEAllows the user to run stored routines
FILEAllows use of SELECT...INTO OUTFILE and LOAD DATA INFILE
INDEXAllows use of CREATE INDEX and DROP INDEX
INSERTAllows use of INSERT
LOCK TABLESAllows use of LOCK TABLES on tables for which the user also has SELECT privileges
PROCESSAllows use of `SHOW FULL PROCESSLIST
RELOADAllows use of FLUSH
REPLICATIONAllows the user to ask where slave or master
CLIENTservers are
REPLICATION SLAVENeeded for replication slaves
SELECTAllows use of SELECT
SHOW DATABASESAllows use of SHOW DATABASE
SHOW VIEWAllows use of SHOW CREATE VIEW
SHUTDOWNAllows use of mysqladmin shutdown
SUPERAllows use of CHANGE MASTER,KILL,PURGE MASTER LOGS,andSET GLOBAL SQL statements. Allowsmysqladmin debug command.Allows one extra connection to be made if maximum connections are reached.
UPDATEAllows use of UPDATE
USAGEAllows connection without any specific privileges

systemctl start mariadb #启动服务 systemctl enable mariadb #设置开机启动 systemctl restart mariadb #重新启动 systemctl stop mariadb.service #停止MariaDB