[关闭]
@nextleaf 2018-11-06T07:55:00.000000Z 字数 3426 阅读 540

在此处输入标题

未分类


20:16 2018/5/14
安装 MariaDB

  1. yum -y install mariadb mariadb-server

MariaDB 安装完毕后,立即启动数据库服务守护进程。
设置 MariaDB 在操作系统重启后自动启动服务。
systemctl start mariadb
systemctl enable mariadb

systemctl status mariadb

对 MariaDB 进行安全配置
通过以下命令进行安全配置,根据实际情况用Y/N回复以下问题:设置 MariaDB 的 root 账户密码,删除匿名用户,禁用 root 远程登录,删除测试数据库,重新加载权限表。

mysql_secure_installation

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.

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.

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.

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.

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.

在配置完数据库的安全配置后,可以通过以下命令查看版本,确认 MariaDB已安装成功。

mysql --version
mysql Ver 15.1 Distrib 5.5.56-MariaDB, for Linux (x86_64) using readline 5.1
可以通过 MariaDB 命令行登录,然后对数据库进行sql查询操作。
mysql -u root -p

配置MariaDB的字符集
文件/etc/my.cnf
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

文件/etc/my.cnf.d/client.cnf
vi /etc/my.cnf.d/client.cnf
在[client]中添加
default-character-set=utf8

文件/etc/my.cnf.d/mysql-clients.cnf
vi /etc/my.cnf.d/mysql-clients.cnf
在[mysql]中添加
default-character-set=utf8

全部配置完成,重启mariadb
systemctl restart mariadb

第四步:为 MariaDB 配置远程访问权限

在第三步中如果禁用 root 远程登录选择 Y 的话就不能在别的电脑通过navicat等工具连接到数据库,这时就需要给对应的 MariaDB 账户分配权限,允许使用该账户远程连接到MariaDB。可以输入以下命令查看账号信息:

select User, host from mysql.user;

root账户中的host项是localhost表示该账号只能进行本地登录,我们需要修改权限,输入命令:

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

修改权限。%表示针对所有IP,password表示将用这个密码登录root用户,如果想只让某个IP段的主机连接,可以修改为:

GRANT ALL PRIVILEGES ON . TO 'root'@'192.168.71.%' IDENTIFIED BY 'my-new-password' WITH GRANT OPTION;

最后别忘了:

FLUSH PRIVILEGES;

保存更改后,再看看用户账号信息:
这个时候发现相比之前多了一项,它的host项是%,这个时候说明配置成功了,我们可以用该账号进行远程访问了。

第五步:CentOS 7 开放防火墙端口

在第四步后如果还是不能远程连上数据库的话应该就是3306端口被防火墙拦截了,这时我们就需要关闭防火墙或者开放防火墙端口。

关闭防火墙:

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall开机启动

开放防火墙端口,开启后要重启防火墙:

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

firewall-cmd --reload

第六步:设置数据库字母大小写不敏感

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

在[mysqld]下加上】

lower_case_table_names=1

默认是等于0的,即大小写敏感。改成1就OK了。如果之前已经建了数据库要把之前建立的数据库删除,重建才生效。

第七步:设置MariaDB数据库默认编码

MariaDB的默认编码是latin1,插入中文会乱码,因此需要将编码改为utf8。

1.登录,使用以下命令查看当前使用的字符集,应该有好几个不是utf8格式。

SHOW VARIABLES LIKE 'character%';

2.修改的配置文件

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

在[client]字段里加入

default-character-set=utf8

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

在[mysqld]字段里加入

character-set-server=utf8

3.重启 MariaDB 配置生效。

systemctl restart mariadb

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注