(1)为安全起见,给数据库的root用户设置密码
[[email protected] ~]# mysql -u root -p //利用root用户登录数据库
Enter password: //新安装的数据库是没有密码的,这里直接按下回车就行
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> set password for 'root'@'localhost' = password('mycisco.cc');
//为root用户设置的密码是mycisco.cc,注意:第二个password和左括号之间是不能有空格的。
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
这样,用户要想登录数据库就必须输入正确的用户名和密码才行。您可在提示符下输入exit退出数据库,然后再重新登录试一试就知道新设置的密码已经生效了。
(2)为其他主机远程连接数据库开放访问权限,重新登入数据库:
MariaDB [(none)]> use mysql; //选择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,password,host from user; //查看user,password,host这三个字段的权限分配情况
+------+-------------------------------------------+-----------------------+
| user | password | host |
+------+-------------------------------------------+-----------------------+
| root | *139EBD293A149D3C25DBD676D882BCBA5A00223E | localhost |
| root | | localhost.localdomain |
| root | | 127.0.0.1 |
| root | | ::1 |
| | | localhost |
| | | localhost.localdomain |
+------+-------------------------------------------+-----------------------+
6 rows in set (0.00 sec)
//通过以上输出可以看出数据库默认只允许用户root在本地服务器(localhost)上登录,不允许其他主机远程连接。
MariaDB [mysql]> grant all privileges on *.* to [email protected]"%" identified by "mycisco.cc";
//上面这条语句将允许用户root使用密码(mycisco.cc)在任何主机上连接该数据库,并赋予该用户所有权限。
Query OK, 0 rows affected (0.01 sec)
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> select user,password,host from user;
+------+-------------------------------------------+-----------------------+
| user | password | host |
+------+-------------------------------------------+-----------------------+
| root | *139EBD293A149D3C25DBD676D882BCBA5A00223E | localhost |
| root | | localhost.localdomain |
| root | | 127.0.0.1 |
| root | | ::1 |
| | | localhost |
| | | localhost.localdomain |
| root | *139EBD293A149D3C25DBD676D882BCBA5A00223E | % | //这行中的“%”就意味着任何主机都被允许连接数据库
+------+-------------------------------------------+-----------------------+
7 rows in set (0.00 sec)
MariaDB [mysql]>
这样数据库的访问权限就设置好了。
6、回到客户端再尝试连接,如图1,显示连接数据库成功。
图1
注:如果连接不成功,可能是服务器中的防火墙没有关闭,可通过指令systemctl disable firewalld 关闭firewalld 并重启系统。