一 host中的含义
/usr/local/mysql/bin/mysql -p'LXYlxy2:024.#8u}' -S /data/mysql/tmp/mysqld.sock
select user,host,authentication_string from mysql.user;
- %:主要允许从任何主机连接到MySQL服务器,即外部连接
- localhost: 代表只允许本地主机连接到MySQL服务器,即本地连接。(本地主机就是MySQL服务器所在的本地主机,就是运行MySQL服务的主机)
- 127.0.0.1:使用TCP/IP的方式进行网络通信,也属于本地连接。(与localhost的区别是,localhoast是可以使用套接字socket进行连接)
上下两张图,可以看出127.0.0.1和localhost区别,localhost可以使用套接字连接,127.0.0.1必须要配合-h -P连接,且可以不带套接字连接
二 修改root用户的host
Mysql8.0,使用root用户修改root的host,使用grant语法会一直报错,只能使用update语法修改
SELECT user, host FROM mysql.user WHERE user='root';
UPDATE mysql.user SET host='new_host' WHERE user='root' AND host='current_host';
TIPS:mysql8.0每个host对应的用户都算新用户,(理解不一定正确)需要重新创建该字段的用户再授权,使用下三中文档创建
三 创建远程用户&远程用户白名单
https://www.cnblogs.com/dongxt/p/14883465.html
##lxy@'10.0.0.10' lxy只能在10.0.0.10这个客户端登录
create user lxy@'10.0.0.10' identified by '123123';
四 修改密码
root用户修改普通用户密码:
mysql> ALTER USER www@'127.0.0.1' IDENTIFIED BY 'www666';
Query OK, 0 rows affected (0.00 sec)
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
普通用户修改自己的密码:在有权限的情况下,直接使用alter修改,set password好像不可以
/usr/local/mysql/bin/mysql -p'www666' -uwww -h127.0.0.1 -P3307
mysql> set password=password('aaa666');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'password('aaa666')' at line 1
mysql> ALTER USER 'www'@'127.0.0.1' IDENTIFIED BY 'aaa666';
Query OK, 0 rows affected (0.02 sec)
五 删除用户
一个用户有两个网段权限,不可以直接drop username;
六 授权规则语法&授权/回收权限
grant all privileges on *.* to lxy@'%' with grant option;
授权 权限 库.表 用户@网段报名单
##创建一个ceshi用户,给予增删改查权限
mysql> create user ceshi@'%' identified by '123123';
Query OK, 0 rows affected (0.03 sec)
mysql> grant SELECT,UPDATE,DELETE,INSERT on *.* to ceshi@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| bbb | % |
| beifen | % |
| ceshi | % |
| slave1 | % |
| www | 127.0.0.1 |
| lxy | localhost |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
| www | localhost |
+------------------+-----------+
11 rows in set (0.00 sec)
#查询权限语句
mysql> show grants for ceshi@'%';
+------------------------------------------------------------------------------+
| Grants for ceshi@% |
+------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO `ceshi`@`%` WITH GRANT OPTION |
+------------------------------------------------------------------------------+
1 row in set (0.00 sec)
#回收delete权限
mysql> REVOKE DELETE ON *.* from ceshi@'%' ;
Query OK, 0 rows affected (0.09 sec)
mysql> show grants for ceshi@'%';
+----------------------------------------------------------------------+
| Grants for ceshi@% |
+----------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE ON *.* TO `ceshi`@`%` WITH GRANT OPTION |
+----------------------------------------------------------------------+
1 row in set (0.00 sec)
七 root用户忘记怎么办
#配置文件加参数,跳过授权表
[mysqld]
skip-grant-tables
然后进去改密码,改完后,记得把配置文件改回来!