Centos7安装MariaDB

1、安装MariaDB

安装命令

yum -y install mariadb mariadb-server

安装完成MariaDB,首先启动MariaDB

systemctl start mariadb

设置开机启动

systemctl enable mariadb

2、初始化MariaDB配置

mysql_secure_installation
[root@localhost ~]# 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用户密码,输入y并回车或直接回车
New password:  <- 设置root用户的密码
Re-enter new password: <- 再次输入root用户的密码
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并回车或者直接回车,表示删除,输入n并回车,表示不删除
 ... 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 <- 是否禁止root远程登录,输入y并回车或直接回车
 ... 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 <- 是否删除test数据库,输出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 <- 是否重新加载权限表,输入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完成,接下来测试登录

mysql -uroot -p
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 5.5.60-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)]> 

3、配置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查看字符集

show variables like "%character%";
show variables like "%collation%";

 

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 5.5.60-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)]> 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)

4、开启root远程访问权限(可选)

这里的123456为你给新增权限用户设置的密码,%代表所有主机,也可以为具体的主机ip地址
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456';
下面一步一定要做,不然无法成功。该句表示从mysql数据库的grant表中重新加载权限数据,因为MySQL把权限都放在了cache中,所以在做完更改后需要重新加载,方能生效。 flush privileges;
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 5.5.60-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)]> 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
MariaDB [mysql]> select User,host from user;
+------+-----------------------+
| User | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | ::1                   |
| root | localhost             |
| root | localhost.localdomain |
+------+-----------------------+
5 rows in set (0.00 sec)
可以看出host默认都是localhost访问权限
MariaDB [mysql]
> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' Query OK, 0 rows affected (0.00 sec) MariaDB [mysql]> flush privileges; Query OK, 0 rows affected (0.00 sec)
再次查询 MariaDB [mysql]
> select User,host from user; +------+-----------------------+ | User | host | +------+-----------------------+ | root | % | | root | 127.0.0.1 | | root | ::1 | | root | localhost | | root | localhost.localdomain | +------+-----------------------+ 5 rows in set (0.00 sec)
发现多了一个用户,该用户所有的主机都可以访问,此时即可远程访问该mysql服务。

 如果防火墙未开启3306端口,也会导致访问失败。

查询是否开启3306端口
[root@localhost ~]# firewall-cmd --query-port=3306/tcp no
防火墙永久开启3306端口 [root@localhost
~]# firewall-cmd --add-port=3306/tcp --permanent success
更新防火墙 [root@localhost
~]# firewall-cmd --reload success
查询是否开启3306端口 [root@localhost
~]# firewall-cmd --query-port=3306/tcp yes

 

posted @ 2019-07-01 22:12  wztone  阅读(474)  评论(0编辑  收藏  举报