Mysql中新建用户,设置密码




Mysql中新建用户,设置密码

新建用户

  • step 1.建立用户:
    CREATE USER 'aaa'@'%' IDENTIFIED BY '123456';表示建立新的用户,名为aaa,新用户密码为123456,'%'表示容许远程登录可是不容许本机登陆
    CREATE USER 'bbb'@'%' IDENTIFED BY '123456';//表示新建立的用户,名为bbb,这个用户密码为123456,能够从其余电脑远程登录mysql所在服务器
    CREATE USER 'ccc'@'%';//表示新建立的用户ccc,没有密码,能够从其余电脑远程登录mysql服务器mysql

  • step 2.受权用户:GRANT ALL PRIVILEGES ON appmetadataDB.* TO 'aaa'@'%';表示将数据库appmetadatadb下的全部表受权给用户aaa。这样用户名aaa就能远程访问到这个数据库(appmetadatadb)下的全部表。写入user表,可是并无及时写入权限表(grant table)。sql

  • step 3.刷新权限表:flush privileges执行这个命令的缘由是,须要将新加入的用户写入到权限表中,即更新grant table
  • step 4.查看以下
 
 
 
  
  
           
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
mysql> select host,user,password from mysql.user; +-----------+--------------+-------------------------------------------+ | host | user | password | +-----------+--------------+-------------------------------------------+ | localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | | 127.0.0.1 | root | | | ::1 | root | | | localhost | | | | % | aaa | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +-----------+--------------+-------------------------------------------+ 10 rows in set (0.02 sec)

这里的mysql.user是一个系统表,任何mysql数据库系统都会存在。
- 检测以下
若是在appmetadatadb数据库所在的系统上登陆用户aaa会报错。错误如右:ERROR 1045 (28000): Access denied for user 'aaa'@'localhost' (using password: YES)。可是将相同的代码放到另外一台主机上登陆,却能够实现登陆。
这个教训必定要记住:就是建立用户时,指定的%是指除了数据库所在主机外的全部主机均可以登陆。好比说,下面这个是用bbb这个用户,在虚拟机上远程访问物理机上的数据库,可是在访问的时候,须要在添加-h [数据库所在主机的IP]字段。数据库

 
 
 
  
  
           
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
[root@littlelawson sbin]# mysql -h 192.168.211.2 -u bbb -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 741 Server version: 5.6.37 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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.

2.修改密码

修改用户密码,方法有不少,这里提供其中的两种:
- set password for [userName]@localhost = password('[NewPassword]'),其中[]中的内容是可变字段。
- update mysql.user set password=password('123') where User="littlelawson" and Host="%";服务器

3.删除用户

  • 命令:drop user [userName]

4.收回权限

5.其它

  • 使用mysql的时候无论输入什么帐户,均可以登陆,可是没法查看其它用户的表【由于没有权限】。
 
 
 
  
  
           
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
C:\Users\enmonster>mysql -u wahaha -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 20 Server version: 5.6.37 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> select user(); +------------------+ | user() | +------------------+ | wahaha@localhost | +------------------+ 1 row in set (0.00 sec)


  • mysql -u root -p命令的意思是:使用帐户root登陆,登陆密码在后面输入。
  • mysql普通用户没法直接经过create database [Database Name]建立数据库,会报错(Access denied for user 'littlelawson'@'localhost' to database 'testfd')。必须在root用户下先建立个数据库再经过受权语句把该database的权限给普通用户。受权语句为:grant all on user.* to user@host identified by password
相关文章
相关标签/搜索