从最新版本的Centos系统开始,默认的是 MariaDB而不是MySQL

使用系统自带的repos安装很简单:

安装

yum install mariadb mariadb-server

启动mariadb

systemctl start mariadb

开机启动

systemctl enable mariadb

设置root密码

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
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 # 正常情况应该输入Y,我这里在自己测试服务器上直接允许远程登陆即可,注意生产环境一定要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] n # 这里不删测试服务器
 ... skipping.

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!

测试登录

mysql -u root -p

得到如下响应,表示成功

[root@hadoop ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.2.11-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

然后后面的操作基本跟mysql一样一样的了。

注意:

虽然前面我们设置过允许root用户远程登陆,但是

MariaDB [(none)]> exit
Bye
[root@hadoop ~]# mysql -h 10.50.200.169 -u root -p
Enter password: 
ERROR 1130 (HY000): Host 'hadoop1' is not allowed to connect to this MariaDB server
[root@hadoop ~]#

还是不能远程登陆的。接着往下看!

关闭防火墙

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

设置远程登陆

grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
flush privileges;

测试

mysql -h 10.50.200.169 -u root -p

这样就可以远程登陆了

[root@hadoop ~]# mysql -h 10.50.200.169 -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.2.11-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit
Bye

注意:

上面操作要成功,必须保证防火墙关闭或者添加端口3306例外;

前面环境中我直接把防火墙关了,但是目前大网络环境下,人们越来越重视安全问题,直接关防火墙是极为不推荐的!

[root@hadoop ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@hadoop ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@hadoop ~]# firewall-cmd --reload
success

把端口3306添加到防火墙例外,然后必须reload一下;过一会可以从其他客户端连接一下,这样就OK了!

在提醒一下,生产环境不要暴露root用户,禁止使用root远程登陆,禁止关闭防火墙!!!

Logo

更多推荐