> ### 新建一个用户joytom,密码设置为123321,并任意远程主机都能访问 | 属性名 | 含义 | | --- | --- | | username | 登陆用户名 | | host | 指定可访问的 ip,如果指定所有 ip 都能访问,将其设为通配符 % 即可。 | | password | 登陆密码,如果密码为空则无需密码 | **2、创建用户** ~~~sql mysql> CREATE USER 'joytom'@'%' IDENTIFIED BY '123321'; Query OK, 0 rows affected (0.00 sec) ~~~ 查看一下是否创建成功: ``` mysql> select user,host from user; +----------+---------------+ | user | host | +----------+---------------+ | copytest | % | | joytom | % | | test | % | | root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | | | vm-8-5-centos | | root | vm-8-5-centos | +----------+---------------+ 10 rows in set (0.00 sec) ``` 3、从另一台服务器上远程登录一下: ``` [root@instance-lzmtqrkn ~]# mysql -h 创建用户的服务器公网ip -P 3306 -u joytom -p123321 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 784 Server version: 5.6.49-log Source distribution Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | +--------------------+ 1 row in set (0.03 sec) ``` 查看一下数据库,发现是没有权限的,只能看到`information_schema`数据库。 > ### **1、给用户授权命令** ``` grant privileges on database.tablename to “username”@’host’; ``` privileges:用户的操作权限,如 SELECT,INSERT,UPDATE 等,如果要授予所的权限则使用 ALL。 ``` privileges 用户的操作权限,如 SELECT,INSERT,UPDATE 等,如果要授予所的权限则使用 ALL。 database 如果不指定数据库,直接 *.* 即可,如果指定数据库但不指定表名,则 database.* 即可。 username 登陆的用户名 host 给予授权的主机 ip,例如我想让用户A的ip使用joytom用户所授予的权限,但是不想让用户B的ip来使用joytom用户的权限。 ``` **2、给 joytom 用户授可查、改的权限。** ``` mysql> grant select,update on copytest.student to "joytom"@'%'; Query OK, 0 rows affected (0.00 sec) ``` **3、另一台服务器去测试:** 发现能看到`copytest`数据库: ``` mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | copytest | +--------------------+ 2 rows in set (0.04 sec) ``` 更多 https://learnku.com/articles/58035