0%

Mariadb安装

记录MariaDB安装,虽然兼容Mysql但是安装的配置文件还是有些区别的

一、yum 方式

安装

1
2
3
4
5
6
7
8
9
#yum直接安装
yum install mariadb-server

#启动并设置开机启动
systemctl start mariadb
systemctl enable mariadb

#查看数据库状态
systemctl status mariadb

配置

如果出现数据库初始化失败,则按照提示信息创建文件目录即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#执行安全性相关任务。你没看错,就是如下这样简单的一句话即可
mysql_secure_installation

#相关选项及含义
#设置系统管理员的密码(root用户),啥也不想设就直接回车即可
Enter current password for root (enter for none):

#是否设置root密码,y
Set root password? [Y/n]

#设置新密码并重复
New password:
Re-enter new password:

#是否不允许匿名用户访问,安全起见,建议选y
Remove anonymous users? [Y/n]

是否拒绝远程登录。这里肯定选n吧。后续肯定会用各种工具远程访问的吧。
Disallow root login remotely? [Y/n]

#是否删除test数据库,根据自己喜好选择即可
Remove test database and access to it? [Y/n]

#重新加载权限表。y即可
Reload privilege tables now? [Y/n]

补充一句,mariadb 不像 MySQL,没有密码复杂度的要求

测试本地登录

先测试本地登录,在命令行执行如下命令,如果看到如下面所示内容,说明本地登录成功

1
mysql -uroot -p

用户管理

安装完毕后,可以新增用户,方法有如下两种

1
2
3
4
5
6
7
# 方法一
MariaDB [(none)]> grant all privileges on *.* to dark@% identified by '1234abcd';
Query OK, 1 row affected, 4 warnings (0.24 sec)

# 方法二
MariaDB [(none)]> insert into mysql.user(user,host,password) values('dark','localhost',password('1234abcd'));
Query OK, 1 row affected, 4 warnings (0.24 sec)

远程登录

关闭本地 firewall 和 iptables 或打开 3306 端口,操作可  点击此处

再查看 mysql 库的 user 表,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

# 先确认一下当前dark用户的host字段值
MariaDB [mysql]> select host, user from user;
+-------------------+------+
| host | user |
+-------------------+------+
| localhost | dark |
| 127.0.0.1 | root |
| ::1 | root |
| base-template0110 | |
| base-template0110 | root |
| localhost | |
| localhost | root |
+-------------------+------+
7 rows in set (0.00 sec)
1
2
3
# 修改dark用户的登录服务器,从localhost 改为 % 通配形式
MariaDB [mysql]> update user set host='%' where host='localhost' and user = 'dark';
Query OK, 1 row affected, 4 warnings (0.24 sec)
1
2
3
# 修改完权限后,执行下面语句刷新权限。或重启服务皆可使新权限生效
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.25 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#查看修改后的数据是否生效,改为 % 说明已生效
MariaDB [mysql]> select host, user from user;
+-------------------+------+
| host | user |
+-------------------+------+
| % | dark |
| 127.0.0.1 | root |
| ::1 | root |
| base-template0110 | |
| base-template0110 | root |
| localhost | |
| localhost | root |
+-------------------+------+
7 rows in set (0.00 sec)

不限制ip

1
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的root密码' WITH GRANT OPTION;

注:**%:表示从任何主机连接到mysql服务器**

FLUSH PRIVILEGES;

限制特定IP才可连接

1
GRANT ALL PRIVILEGES ON *.* TO 'root'@'100.100.100.100(此处为你的公网IP)' IDENTIFIED BY '你的root密码' WITH GRANT OPTION;

FLUSH PRIVILEGES;

修改配置文件

/etc/my.cnf.d 目录下的 mariadb-server.cnf 文件中

1
bind-address = 127.0.0.1

设置字符集

这部分关系到后续创建的库和表的字符集及插入数据的正确性。所以请务必小心

1
2
3
4
5
6
7
8
9
10
11
12
#在/etc/my.cnf中的[mysql]下设置字符集,如下所示

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

#配置字符集
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 中的 [client] 标签下添加如下内容

1
2
3
4
5
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server

[client]
default-character-set=utf8

 在 /etc/my.cnf.d/mysql-clients.cnf 中的 [mysql] 标签下添加如下内容

1
2
3
4
5
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility

[mysql]
default-character-set=utf8

以上三处修改完毕后,重启服务  systemctl restart mariadb。然后登录 mariadb 执行如下命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 执行 show variables like "%character%";
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/ |
+--------------------------+----------------------------+
1
2
3
4
5
6
7
8
9
# 执行 show variables like "%collation%";
MariaDB [(none)]> show variables like "%collation%";
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
-------------The END-------------