MySQL学习笔记16

MySQL的用户管理和权限管理:

在MySQL中创建更多的用户,不可能都使用root用户。root用户的权限太高,一个误操作,容易造成很大的失误。

注意:MySQL中不能单纯通过用户名来说明用户,必须加上主机,如[email protected]

1、语法:

创建用户的时候,直接设置密码。

mysql> create user '用户名'@'被允许连接的主机名称或主机的IP地址' identified by '用户密码';
mysql> select user,host from mysql.user;

2、创建MySQL账号,用户名tom,用户密码:123

mysql> create user 'tom'@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
| tom           | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

该tom用户只能本地访问。localhost本机访问。

我们通过navicat软件进行登录,发现登录不上,这是因为root账号只能本地登录。

MySQL学习笔记16_第1张图片

案例:创建一个mysql账号,要求开通远程连接。主机ip地址192.168.17.125,用户名harry,密码:123。

create user 'harry'@'192.168.17.125' identified by '123';

然后在192.168.17.125服务器上进行测试:

[root@mysql-test ~]# mysql -h 192.168.17.123 -uharry -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.43 MySQL Community Server (GPL)

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

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

MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)

说明:

yum install mysql -y

yum安装mysql:代表安装的是mysql客户端;

yum安装mysql-server:代表安装的mysql-server服务器端;

案例:创建一个MySQL账号(要求开通远程连接),主机IP的网段:10.1.1.0,用户名:jack,用户密码:123

mysql> create user 'jack'@'192.168.17.%' identified by '123';
Query OK, 0 rows affected (0.04 sec)

mysql>
mysql> select host, user from mysql.user;
+----------------+---------------+
| host           | user          |
+----------------+---------------+
| 192.168.17.%   | jack          |
| 192.168.17.125 | harry         |
| localhost      | mysql.session |
| localhost      | mysql.sys     |
| localhost      | root          |
| localhost      | tom           |
+----------------+---------------+
6 rows in set (0.00 sec)



[root@mysql-test ~]# mysql -h 192.168.17.123 -ujack -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.43 MySQL Community Server (GPL)

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

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

MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)

案例:创建一个MySQL账号(要求开通远程连接),要求面向所有主机开放,用户名:root,用户密码:123。

mysql> create user 'root'@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> select host, user from mysql.user;
+----------------+---------------+
| host           | user          |
+----------------+---------------+
| %              | root          |
| 192.168.17.%   | jack          |
| 192.168.17.125 | harry         |
| localhost      | mysql.session |
| localhost      | mysql.sys     |
| localhost      | root          |
| localhost      | tom           |
+----------------+---------------+
7 rows in set (0.00 sec)

MySQL里面的账号不能仅仅看账号,也要看IP地址。由这两个决定一个账号。

删除账号:

基本语法:

mysql> drop user '用户名'@'主机名称或主机的IP地址';
特别说明:
如果在删除用户时没有指定主机的名称或主机的IP地址,则默认删除这个账号的所有信息。

案例:删除tom这个账号。

mysql> drop user 'tom'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> select host, user from mysql.user;
+----------------+---------------+
| host           | user          |
+----------------+---------------+
| %              | root          |
| 192.168.17.%   | jack          |
| 192.168.17.125 | harry         |
| localhost      | mysql.session |
| localhost      | mysql.sys     |
| localhost      | root          |
+----------------+---------------+
6 rows in set (0.00 sec)

案例:删除jack这个账号。

mysql> drop user 'jack'@'192.168.17.%';
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> select host, user from mysql.user;
+----------------+---------------+
| host           | user          |
+----------------+---------------+
| %              | root          |
| 192.168.17.125 | harry         |
| localhost      | mysql.session |
| localhost      | mysql.sys     |
| localhost      | root          |
+----------------+---------------+
5 rows in set (0.00 sec)

扩展:删除MySQL账号的另外一种方式。

mysql> delete from mysql.user where user='root' and host='%';
Query OK, 1 row affected (0.00 sec)

mysql> select host, user from mysql.user;
+----------------+---------------+
| host           | user          |
+----------------+---------------+
| 192.168.17.125 | harry         |
| localhost      | mysql.session |
| localhost      | mysql.sys     |
| localhost      | root          |
+----------------+---------------+
4 rows in set (0.00 sec)

修改MySQL账号:

特别说明:MySQL用户重命名通常可以更改两部分,一部分是用户的名称,一部分是被允许访问的主机名称或主机的IP地址。

基本语法:

mysql> rename user 旧用户信息 to 新用户信息;

案例:把用户'root'@'%'更改为'root'@'10.1.1.%'。

mysql> create user 'root'@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> select host, user from mysql.user;
+----------------+---------------+
| host           | user          |
+----------------+---------------+
| %              | root          |
| 192.168.17.125 | harry         |
| localhost      | mysql.session |
| localhost      | mysql.sys     |
| localhost      | root          |
+----------------+---------------+
5 rows in set (0.00 sec)


mysql> rename user 'root'@'%' to 'root'@'192.168.17.%';
Query OK, 0 rows affected (0.00 sec)

mysql> select host, user from mysql.user;
+----------------+---------------+
| host           | user          |
+----------------+---------------+
| 192.168.17.%   | root          |
| 192.168.17.125 | harry         |
| localhost      | mysql.session |
| localhost      | mysql.sys     |
| localhost      | root          |
+----------------+---------------+
5 rows in set (0.00 sec)

mysql> create user 'tom'@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)

mysql> rename user 'tom'@'localhost' to 'harry'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> select host, user from mysql.user;
+----------------+---------------+
| host           | user          |
+----------------+---------------+
| 192.168.17.%   | root          |
| 192.168.17.125 | harry         |
| localhost      | harry         |
| localhost      | mysql.session |
| localhost      | mysql.sys     |
| localhost      | root          |
+----------------+---------------+
6 rows in set (0.00 sec)

扩展:使用update语句更新用户信息

mysql> select host, user from mysql.user;
+----------------+---------------+
| host           | user          |
+----------------+---------------+
| 192.168.17.%   | root          |
| 192.168.17.125 | harry         |
| localhost      | harry         |
| localhost      | mysql.session |
| localhost      | mysql.sys     |
| localhost      | root          |
+----------------+---------------+
6 rows in set (0.00 sec)



mysql> update mysql.user set user='tom', host='localhost' where user='harry' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

你可能感兴趣的:(MySQL,mysql)