4

CentOS中用Yum安装MariaDB

 2 years ago
source link: https://hypersharp.net/ops/83-yum-install-mariadb-for-centos/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在CentOS,RHEL之类的系统上,安装MariaDB最简单的方式是使用Yun命令。

配置yum源

首先我们需要在以下位置创建yum源文件

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

在文件内插入以下内容:

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

运行命令安装MariaDB

yum -y install MariaDB-server MariaDB-client
yum -y install MariaDB-server MariaDB-client

成功安装后,可以通过以下命令控制MariaDB服务:

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

使用 mysql_secure_installation 命令配置服务器

mysql_secure_installation # 运行配置命令
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.
Set root password? [Y/n] y # 设置root密码
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] Y # 是否禁止root用户远程登陆
... Success!
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.
Thanks for using MariaDB!
mysql_secure_installation   # 运行配置命令

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.

Set root password? [Y/n] y  # 设置root密码
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] Y # 是否禁止root用户远程登陆
 ... Success!

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.

Thanks for using MariaDB!

配置MariaDB的字符集

查看/etc/my.cnf文件内容,其中包含一句 !includedir /etc/my.cnf.d 说明在该配置文件中引入 /etc/my.cnf.d 目录下的配置文件。

1)使用 vi 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
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 目录下无 server.cnf 文件,则直接在 /etc/my.cnf 文件的 [mysqld] 标签下添加以上内容。

2)用 vi client.cnf 命令编辑 /etc/my.cnf.d/client.cnf 文件,在[client] 标签下添加:

default-character-set=utf8
default-character-set=utf8

3)用 vi mysql-clients.cnf 命令编辑 /etc/my.cnf.d/mysql-clients.cnf 文件,在 [mysql] 标签下添加:

default-character-set=utf8
default-character-set=utf8

配置完成后 systemctl restart mariadb 重启服务。进入到数据库查看字符设置。

MariaDB [(none)]> show variables like "%character%";
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
MariaDB [(none)]> show variables like "%collation%";
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> show variables like "%character%";
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]> show variables like "%collation%";
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

添加用户,设置权限

创建数据库,创建用户并授予权限:

CREATE DATABASE IF NOT EXISTS hypersharp default character set utf8mb4 COLLATE utf8mb4_unicode_ci;
create user hypersharp;
USE mysql;
update user set Password = PASSWORD("********") where user='hypersharp';
grant all privileges on hypersharp.* to hypersharp@localhost identified by '********';
CREATE DATABASE IF NOT EXISTS hypersharp default character set utf8mb4 COLLATE utf8mb4_unicode_ci;
create user hypersharp;
USE mysql;
update user set Password = PASSWORD("********") where user='hypersharp';
grant all privileges on hypersharp.* to hypersharp@localhost identified by '********';

授予指定用户外部访问权限:

grant all on *.* to 'hypersharp'@'10.153.176.106/255.255.255.0' identified by '********';
grant all on *.* to 'hypersharp'@'10.137.42.136/255.255.255.0' identified by '********';
flush privileges;
grant all on *.* to 'hypersharp'@'10.153.176.106/255.255.255.0' identified by '********';
grant all on *.* to 'hypersharp'@'10.137.42.136/255.255.255.0' identified by '********';
flush privileges;

参考资料:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK