文章目录
环境
> cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
前言
原本寄希望于直接使用yum
,安装MariaDB
,yum search mariadb
也可以搜索到,可直接安装的版本比较低(使用的阿里的镜像):
Server version: 5.5.64-MariaDB MariaDB Server
翻阅官方说明,可以自定义yum
的MariaDB.repo
安装。
1. 自定义MariaDB.repo
MariaDB官网提供了创建一个MariaDB.repo
的工具页:
-
选择自己的服务器的版本和想要安装的版本:
-
页面下会生成一个
MariaDB.repo
文件内容,如下:
# MariaDB 10.4 CentOS repository list - created 2019-11-19 02:02 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
- 按照官方提示,在
/etc/yum.repos.d/
目录下,创建MariaDB.repo
文件。 - 官网的下载地址在国内访问比较慢,可以修改为国内镜像:
# MariaDB 10.4 CentOS repository list - created 2019-11-19 02:02 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://mirrors.ustc.edu.cn/mariadb/yum/10.4/centos7-amd64
gpgkey=http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
2. yum 安装mariaDB
- 安装
- 客户端 mariadb-client
- 服务端 mariadb-server
yum install mariadb-client mariadb-server
- 如果需要开发环境(如C\C++等),安装开发环境
yum install mariadb-devel
2. 配置启动项
# 启动mariadb
systemctl start mariadb
# 设置为开机启动
systemctl enable mariadb
3. 初始化mariaDB
- 输入命令,启动配置首次安装选项:
mysql_secure_installation
- 根据提示配置mariaDB
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
# 输入密码,新安装的没有密码,直接回车
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
# 是否设置root的密码,设置一个密码
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
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] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
# 是否禁止root远程登录
Disallow root login remotely? [Y/n] n
... skipping.
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.
# 是否移除默认安装时的测试DB
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
# 是否重新加载权限表。即以上改变是否立即生效,重启mariaDB也可以
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
4. 修改配置
4.1 配置 /etc/my.cnf
- 根据需要增加以下配置:
[mysqld]
# 表名一律小写,即新建表和查询SQL时,表名转换为小写。
# 相当于表名忽略大小写,但如果之前大写创建的,该方式并不能解决SQL的表大小写问题。
lower_case_table_name = 1
# 允许上传的最大数据包大小
max_allowed_packet = 10M
# 跳过DNS反向解析
skip-name-resolve
# 开启慢查询日志
slow-query-log = 1
slow-query-log-file = /var/log/mysql/mysql-slow.log
# 超过多少秒的SQL记录为慢查询
long_query_time = 1
# 修改字符集为utf8mb4,主要为解决Emoji
init-connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
- 修改后重启生效
systemctl restart mariadb
4.2 修改远程登录
如果需要远程登录的,需要修改防火墙和数据库权限表。
修改防火墙
1. 查看端口开放情况
firewall-cmd --query-port=3306/tcp
2. 开放端口
根据情况,选择合适的防火墙策略
- 方式一: 开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
- 方式二: 设置白名单
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="3306" accept"
3. 使防火墙设置生效
firewall-cmd --reload
4. 查看防火墙规则
firewall-cmd --list-all
修改权限表
1. 本机通过命令登录mysql,输入密码登录
mysql -u root -p
2. 修改mysql
数据库的user
host
- 切换到mysql数据库下
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
- 时间看user表
MariaDB [mysql]> select host,user from user;
+-----------------------------------------+------+
| host | user |
+-----------------------------------------+------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | root |
| vm-85972bf5-6119-48e8-b7ee-95f6d88312c2 | root |
+-----------------------------------------+------+
4 rows in set (0.00 sec)
- 将host本机名的项改为
%
,任意IP连接
MariaDB [mysql]> update user set host='%' where host='vm-85972bf5-6119-48e8-b7ee-95f6d88312c2';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
- 刷新权限退出
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> quit;
Bye
- 以上,通过远程连接,测试是否成功。