mysql权限机制

一、用户管理

1.设置密码

#mysqladmin  -u  root  password  "123"

2.修改密码

(1)root 用户修改自己的密码

方法一:#mysqladmin  -u  root  -p123  password  "456"      ----123为旧密码,456为新密码

方法二:适用于Mysql5.7版本

#mysql> update mysql.user set authentication_string=password('新密码') where user='root' and host='localhost';   

方法三:set  password=password('new_password');
上面方法将会在后面的版本remove,新版本5.7使用下面方法
set   password='new_password';

(2)root 修改其他用户的密码

方法一:

mysql> set  password   for  'user3'@'localhost'=password('new_password');

上面的方法会在将来remove,使用下面的方法:
mysql> set  password   for  user3@'localhost'='new_password';

方法二:适用于Mysql5.7版本

update   mysql.user  set   authentication_string=password('new_password')   where  user='user3' AND host='localhost';

方法三:wing普通用户修改自己密码 

先用wing帐号登入到数据库中

mysql> set   password=password('new_password'); 
mysql> alter  user  'wing'@'localhost'  identified by 'new_password';   用修改user表的方式,适用于5.7及以后版本

3.登陆Mysql

#mysql   -h10.18.41.22  -P  3306  -u root  -p123

-h  指定主机名   【默认为localhost】

-P  mysql服务器端口  【默认3306】

-u   指定用户名      【默认root】

-p   指定登陆密码    【默认为空密码】


#mysql -h10.18.41.22 -P 3306 -u root -p123 mysql -e 'select user,host from user'

此处mysql为指定登陆的数据库

-e     接SQL语句


4.创建用户

(1)方法一:create  user语句创建(用户只能登陆,没有其他权限)

mysql>create  user  wing@'10.18.41.%' identified  by '123';      ---%所有

(2)方法二:grant语句创建(创建用户并给用户授权)

mysql>grant  all   on  *.*  to  'bijz'@'10.18.41.%'  identified  by  '123'

(3)撤销权限

方法一(命令直接撤销权限): mysql>revoke  all on *.*  from  'bijz'@'10.18.41.%';      ----------撤销权限

方法二(手动更新权限表user):mysql>select *  from  user  \G  查看mysql库中的user表中有哪些权限设置

                                                      mysql>delete  from  user  where  host='10.18.41.%';   先删除user表中的记录

                                                      mysql>flush  privileges;    当手动更新权限表时,需要刷新授权表

解释:     *.*   -----库.表(列)

5.删除用户

方法一:drop  user语句删除

mysql>drop   user  'wing'@'10.18.41.%';

方法二:delete 语句删除(从mysql库的user表中删除创建的用户)

mysql>delete  from   mysql.user  where  user='wing'  and  host='10.18.41.%';

mysql>flush  privileges;    刷新授权


二、远程登陆

1.创建用户:

mysql> create user  tom@'%'  identified  by  '123';

解释:%表示所有主机但是不包括127.0.0.1和localhost,这两个需要单独设置

mysql>create user  tom@'127.0.0.1'  identified  by '123';

#mysql  -h 127.0.0.1  -u tom  -p123

三、端口修改

#vim  /etc/my.cnf     ----修改mysql的配置文件

[mysqld]

port=3307   ---默认3306

#systemctl  restart  mariadb

注意:本地登陆不需要添加端口号。

针对mariadb5.5版本-----本地登陆,端口不生效,任何端口都可以,远程登陆必须使用修改的端口号登陆

四、忽略大小写

#vim /etc/my.cnf

[mysqld]

lower_case_table_name=1     ------实现表名不区分大小写

注意:在mysql中除命令关键字不区分大小写外其他的区分大小写。

五、权限控制机制

        四张表:  user    db   (5.1/5.5 版本有host表)   tables_prive    columns_priv

user表:

    user表示 Mysql 中最重要的一个权限表,记录允许连接到服务器的账号信息,里面的权限是全局级的。

db表:

    db表中存储了用户对某个数据库的操作权限,决定用户能从哪个主机存取哪个数据库。

host表:控制远程的登陆权限

tables_priv表:

    tables_prive表用来对表设置操作权限。

columns_prive表:

    columns_prive表用来对表的某一列设置权限。

1.用户认证

   查看mysql.user表

2.权限(privilege)认证

    先查看user  ----->db  ----->tables_prive  ------>columns_prive

3.刷新权限:两种方式

修改表之后需要刷新权限
方式1:
mysql > flush privileges;

方式2:
# mysqladmin flush-privileges -u root -p1

4.查看权限

查看自己的权限:

mysql>show  grants \G

查看别人的权限:

mysql>show  grants  for  bijz@'%' \G

查看所有的权限:

mysql>select  *  from  mysql.user \G


六、丢失root用户的密码

#vim  /etc/my.cnf

[mysqld]
skip-grant-tables
# service mysqld restart
# mysql -uroot
mysql> UPDATE mysql.user SET authentication_string=password(‘new_password’)
WHERE user=’root’ AND host=’localhost’;
mysql> FLUSH PRIVILEGES;




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值