MariaDB-10安装

Anyhong 2019年03月01日 4,131次浏览

本文是在 CentOS 7 中安装 MariaDB(MySql)的一些常见操作步骤, 在 CentOS 7 默认的 yum 源中是没有 MariaDB 数据源的,所以要么采用手动下载安装包进行安装,要么手动添加 MariaDB 的 yum 下载数据源,再使用 yum 命令进行安装,这里使用的就是后面这种方式。

创建 MariaDB.repo

在 CentOS 7 的 /etc/yum.repos.d/ 目录下创建 MariaDB 的下载源配置文件 MariaDB.repo

vi /etc/yum.repos.d/MariaDB.repo

添加数据库下载源信息然后保存:

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

安装 MariaDB

使用 yum 命令安装 MariaDB:

yum install MariaDB-server MariaDB-client -y

安装完成后启动数据库:

systemctl start mariadb

设置开机启动:

systemctl enable mariadb

查看 MariaDB 服务状态:

systemctl status mariadb

MariaDB 安全配置

使用如下命令对 MariaDB 进行安全配置:

mysql_secure_installation

安装配置过程中会依次提醒如下配置,根据自己的实际需要来选择 Y 或者 N

  1. 设置 MariaDB 的 root 密码

    Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.
    Set root password? [Y/n]

  2. 删除匿名用户

    By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
    Remove anonymous users? [Y/n]

  3. 禁用 root 远程登录

    Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
    Disallow root login remotely? [Y/n]

  4. 删除测试数据库

    By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
    Remove test database and access to it? [Y/n]

  5. 重新加载权限列表使生效

    Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
    Reload privilege tables now? [Y/n]

远程访问数据库

如果在上一步安全配置中禁用了 root 远程登录,此时 MariaDB 就只能在本地连接登录了,如果要使用远程连接到数据库,这时候就需要给 MariaDB 的账户配置远程登录权限。

进入 MariaDB 数据库控制台

使用如下命令登录数据库进入数据库控制台:

mysql -u root -p

成功后可以看到如下提示:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.2.16-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

设置远程访问权限

使用如下 sql 语句查询账户权限:

select User, host from mysql.user;

可以看到 root 账户全是本地权限:

+------+---------------------+
| User | host                |
+------+---------------------+
| root | 127.0.0.1           |
| root | ::1                 |
|      | localhost           |
| root | localhost           |
|      | vm\_10\_254\_centos |
| root | vm\_10\_254\_centos |
+------+---------------------+

使用如下命令修改权限,命令中的 % 表示所有地址:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

如果让指定的 ip 段或者 ip 地址能够访问数据库,使用如下命令:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.0.%' IDENTIFIED BY 'password' WITH GRANT OPTION;
或者
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.0.1' IDENTIFIED BY 'password' WITH GRANT OPTION;

最后使用让配置生效:

FLUSH PRIVILEGES;

最后再查看一下用户权限,发现 root 账户下多了一项 [host 为 “%”] 的配置 , 说明允许所有地址远程连接数据库的权限配置成功了:

+------+---------------------+
| User | host                |
+------+---------------------+
| root | %                   |
| root | 127.0.0.1           |
| root | ::1                 |
|      | localhost           |
| root | localhost           |
|      | vm\_10\_254\_centos |
| root | vm\_10\_254\_centos |
+------+---------------------+

防火墙端口开放

如果远程还是连接不上数据库的 3306 端口,那么有可能是 3306 端口被 CentOS 7 防火墙拦截了,这时候需要再配置防火墙,开放 3306 端口。

关闭防火墙:

systemctl stop firewalld.service

禁止防火墙开机启动:

systemctl disable firewalld.service 

防火墙开放 3306 端口:

firewall-cmd --zone=public --add-port=3306/tcp --permanent

重启防火墙:

firewall-cmd --reload

其它配置

数据库字母大小不敏感设置

项目部署到 CentOS 7 上发现一直连接不上数据库,一直报找不到数据表和字段,最后发现是数据表大小写的问题,所以需要配置数据库大小写不敏感:

打开数据库配置文件:

vi /etc/my.cnf.d/server.cnf

[mysqld] 后面加上大小写不敏感配置:

lower_case_table_names=1

说明:
lower_case_table_names 等于 0,就是大小写敏感,等于 1 就是大小写不敏感,默认是等于 0 的。

设置数据库默认编码

查看字符集

进入数据库命令控制台查看数据库使用的字符集:

SHOW VARIABLES LIKE 'character%';

发现有几个地方默认使用的是 latin1 编码方式,插入中文会产生乱码,所以需要改成 utf8 格式:

MariaDB [(none)]> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

修改字符集配置

打来 client 配置文件 client.cnf

vi /etc/my.cnf.d/client.cnf

找到 [client] 字段,并在后面加上:

default-character-set=utf8

打开 server 配置文件 server.cnf

vi /etc/my.cnf.d/server.cnf

找到 [mysqld] 字段,并在后面加上:

character-set-server=utf83

重启数据库使配置生效:

systemctl restart mariadb