Mysql--基本配置、授权及删除权限

Mac下安装与配置MySQL

启动或停止

启动Mysql服务端

sudo /usr/local/mysql/support-files/mysql.server start

启动Mysql客户端

sudo /usr/local/mysql/support-files/mysql.server stop

mac偏好设置启动or关闭


Mysql--基本配置、授权及删除权限_第1张图片

配置文件设置

复制cnf文件

cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf     //复制默认mysql配置 文件到/etc目录 

配置文件路径 /etc/my.cnf

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

[client]  # 服务端设置,终端输入mysql 无需输入密码 编码设置
default-character-set=utf8

user='root'
password='456'

[mysql]
default-character-set=utf8

配置完成后,需要重启服务端才能生效

环境变量配置

在您相应的配置文件里添加,.profile or .zshrc

PATH=$PATH:/usr/local/mysql/bin

修改密码

update mysql.user set authentication_string=password('123') where user='root' and host='localhost’;
flush privileges; # 写入设置

创建用户及授权

Mysql--基本配置、授权及删除权限_第2张图片
授权�解释
授权表
user #该表放行的权限,针对:所有数据,所有库下所有表,以及表下的所有字段
db #该表放行的权限,针对:某一数据库,该数据库下的所有表,以及表下的所有字段
tables_priv #该表放行的权限。针对:某一张表,以及该表下的所有字段
columns_priv #该表放行的权限,针对:某一个字段

按图解释:
user:放行db1,db2及其包含的所有
db:放行db1,及其db1包含的所有
tables_priv:放行db1.table1,及其该表包含的所有
columns_prive:放行db1.table1.column1,只放行该字段
创建用户
create user 'buyi'@'1.1.1.1' identified by '123';
create user 'buyi'@'192.168.1.%' identified by '123';
create user 'buyi'@'%' identified by '123';
授权
授权:对文件夹,对文件,对文件某一字段的权限
查看帮助:help grant
常用权限有:select,update,alter,delete
all可以代表除了grant之外的所有权限

针对所有库的授权:*.*
grant select on *.* to 'buyi1'@'localhost' identified by '123'; #只在user表中可以查到buyi1用户的select权限被设置为Y

针对某一数据库:db1.*
grant select on db1.* to 'buyi2'@'%' identified by '123'; #只在db表中可以查到buyi2用户的select权限被设置为Y

针对某一个表:db1.t1
grant select on db1.t1 to 'buyi3'@'%' identified by '123';  #只在tables_priv表中可以查到buyi3用户的select权限

针对某一个字段:
mysql> select * from t3;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | buyi1 |   18 |
|    2 | buyi2 |   19 |
|    3 | buyi3 |   29 |
+------+-------+------+

grant select (id,name),update (age) on db1.t3 to 'buyi4'@'localhost' identified by '123'; 

可以在tables_priv和columns_priv中看到相应的权限
mysql> select * from tables_priv where user='buyi4'\G

mysql> select * from columns_priv where user='buyi4'\G
删除权限
revoke select,update on db1.student from 'lili'@localhost;
mysql> use 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
mysql> select host,user from user;
+-----------+-----------+
| host      | user      |
+-----------+-----------+
| %         | buyi      |
| localhost | jack      |
| localhost | lili      |
| localhost | mysql.sys |
| localhost | root      |
| localhost | tom       |
+-----------+-----------+
6 rows in set (0.00 sec)

mysql> select * from tables_priv;
+-----------+-----+-----------+------------+----------------+---------------------+---------------+---------------+
| Host      | Db  | User      | Table_name | Grantor        | Timestamp           | Table_priv    | Column_priv   |
+-----------+-----+-----------+------------+----------------+---------------------+---------------+---------------+
| localhost | sys | mysql.sys | sys_config | root@localhost | 2017-09-04 11:37:51 | Select        |               |
| localhost | db1 | lili      | student    | root@localhost | 0000-00-00 00:00:00 | Select,Update |               |
| localhost | db1 | jack      | teacher    | root@localhost | 0000-00-00 00:00:00 | Select        |               |
| localhost | db1 | tom       | course     | root@localhost | 0000-00-00 00:00:00 |               | Select,Update |
+-----------+-----+-----------+------------+----------------+---------------------+---------------+---------------+
4 rows in set (0.00 sec)

mysql> select * from columns_priv;
+-----------+-----+------+------------+-------------+---------------------+---------------+
| Host      | Db  | User | Table_name | Column_name | Timestamp           | Column_priv   |
+-----------+-----+------+------------+-------------+---------------------+---------------+
| localhost | db1 | tom  | course     | name        | 0000-00-00 00:00:00 | Select,Update |
| localhost | db1 | tom  | course     | 周期        | 0000-00-00 00:00:00 | Select,Update |
+-----------+-----+------+------------+-------------+---------------------+---------------+
2 rows in set (0.00 sec)


mysql> revoke select,update on db1.student from 'lili'@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from tables_priv;
+-----------+-----+-----------+------------+----------------+---------------------+------------+---------------+
| Host      | Db  | User      | Table_name | Grantor        | Timestamp           | Table_priv | Column_priv   |
+-----------+-----+-----------+------------+----------------+---------------------+------------+---------------+
| localhost | sys | mysql.sys | sys_config | root@localhost | 2017-09-04 11:37:51 | Select     |               |
| localhost | db1 | jack      | teacher    | root@localhost | 0000-00-00 00:00:00 | Select     |               |
| localhost | db1 | tom       | course     | root@localhost | 0000-00-00 00:00:00 |            | Select,Update |
+-----------+-----+-----------+------------+----------------+---------------------+------------+---------------+
3 rows in set (0.00 sec)

mysql> revoke select on db1.teacher from 'jack'@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> revoke select(name,周期),update(name,周期) on db1.course from 'tom'@localhost;
Query OK, 0 rows affected (0.00 sec)
查看账户信息
select * from user\G
删除用户
mysql> drop user jack@localhost;
Query OK, 0 rows affected (0.00 sec)

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

你可能感兴趣的:(Mysql--基本配置、授权及删除权限)