[root@localhost ~]# mysql -u root -p //进入数据库
mysql> grant all on *.* to 'admin'@'%' identified by 'admin123'; //这是一条授权的命令,允许外来的数据访问,登录账户为admin,密码为admin123。这里的%的意思远程登录意思
数据库用户授权格式为:GRANT 权限列表 ON 数据库名.表名 TO 用户名@来源地址 [IDENTIFIED BY ‘密码’] ,
授权之后我们用外部软件Navicat for MySQL 软件进行连接
连接进去之后就是以上页面,他可以很直观的看到数据库和表的信息
mysql> create database yang; //比如创建一个yang的数据库
Query OK, 1 row affected (0.00 sec)
mysql> show databases; //这是系统展示出来的库的信息,能看见yang的库已经创建成功了。
+--------------------+
| Database |
+--------------------+
| information_schema |
| auth |
| myadm |
| mysql |
| performance_schema |
| sys |
| yang |
+--------------------+
7 rows in set (0.00 sec)
mysql> use yang; //使用yang的数据库
Database changed
mysql> show tables; //展示表的信息,发现没有任何的表
Empty set (0.00 sec)
mysql> create table cu(zhangsan char(16) not null,lisi char(32) default'',primary key (zhangsan));
Query OK, 0 rows affected (0.01 sec)
//这边是创建一个表 ,not mull 意思是不能为空 ,char :字节 ,default:默认,primary key:主标识
注:有一点需要注意创建一个表是必须要有字节信息,在()里面设置好了一张表的格式,每个字段代表一列,这里也限制之后如果插入新数据信息,对于列不能超过创建好的列的个数
以上 图片是我在系统创建好的一张表和其信息
mysql> use yang; //使用yang的库
Database changed
mysql> show tables; //展示表的信息,发现只有一个cu的库
+----------------+
| Tables_in_yang |
+----------------+
| cu |
+----------------+
1 row in set (0.00 sec)
mysql> drop table yang.cu;
Query OK, 0 rows affected (0.00 sec)
mysql> drop database yang
mysql> show databases; //发现yang的数据库已经被删除了
+--------------------+
| Database |
+--------------------+
| information_schema |
| myadm |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database auth; //这边在重新创建一个yang的库
Query OK, 1 row affected (0.00 sec)
mysql> use auth; //使用auth的数据库
Database changed
mysql> create table users(user_name CHAR(16) NOT NULL,user_passwd CHAR(48) DEFAULT'',PRIMARY KEY (user_name));
Query OK, 0 rows affected (0.00 sec)
INSERT INTO表名(字段1,字段2,..)VALUES(字段1的值,字段2的值,....…)
insert into li(user name,user passwd) values('zhangsan', password(123456'); ###标准使用方法
insert into users values('lisi',, password(123456')); ###如果这条记录包含表中所有字段的值,则插入语句中的制定字段可以省略
#######举例#####
mysql> use auth; ####进入auth这个数据库
Database changed
mysql>insert into users(user_name,user_passwd) values('zhangsan', password('123456'));
Query oK, 1 row affected,1 warning(0.01 sec)
mysql> insert into users values('lisi',password(123456'));
Query oK.1 row affected,1 warning (0.01 sec)
UPDATE 表名 SET 字段名1=值1 [,字段名2=值2] WHERE 条件表达式
update auth.users set user_passwd=password(123456') where user_name='lisi';
update mysql.user set authentication_string=PASSWORD(abc123') where User= 'root'; ##mysql5.7版本修改root的命令
############举例#########
mysql> update auth.users set user_passwd=password('abc123') where user_name='lisi; ###修改lisi的密码
Query OK,1 row affected,1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql>flush privileges; ####刷新下
DELETE FROM表名 WHERE 条件表达式
delete from auth.users where user_name='lisi';
######举例####
mysql> select* from auth.users;
mysql> delete from auth.users where user_name='lisi';
mysql> select* from auth.users;
从数据表中查找符合条件的数据记录
SELECT字段名1,字段名2......FROM表名WHERE条件表达式
select * from auth.users;
select user_name,user_passwd from auth.users where user_name='zhangsan';
######列子######
mysql> select* from auth.users; ###查看数据库auth中users表中所有的数据,这边用通配符*表示所有
######列子######
mysql>select user_name,user_passwd from auth.users where user_name='zhangsan'; ####查看数据库auth的users表中zhangsan数据
mysql> select * from auth.users; ###查询lisi的信息
设置用户权限(用户不存在时,则新建用户)
GRANT权限列表ON数据库名.表名TO用户名@来源地址[IDENTIFIED BY‘密码’]
grant select on auth.* to 'xiaoqi'@'localhost' identified by '123456';
######列子####
mysql>grant select on auth.* to 'xiaoqi'@'localhost' identified by '123456';
Query oK,O rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK,0 rows affected (0.00 sec)
[root@localhost~]# mysql -u xiaoqi -p ##登录
Enter password:
Welcome to the MySQL monitor.Commands end with ; or lg.
Your MySQL connection id is 10
Server version:5.7.20 Source distribution
Copyright (c)2000,2017,Oracle and/or its affiliates.All rights reserved.
create database bdqn;
grant all on auth.* to 'dbuser'@'192.168.100.%'identified by 'pwd@123':
grant create on bdqn.* to 'dbuser'@'192.168.100.1' identified by 'pwd@123;
#################举例###########
mysql> create database bdqn; ##创建bdqn数据库####
Query OK,1 row affected(0.02 sec)
mysql>grant all on bdqn.* to 'dbuser'@'192.168.100.1' identified by 'pwd@123';
Query OK,0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges:
Query OK,0 rows affected(0.00 sec)
sHOW GRANTS FOR用户名回来源地址
##并###列子########
mysql> show grants for 'dbuser'@'192.168.100.1'; ###查看用户的权限
REVOKE权限列表ON数据库名.表名FROM用户名@来源地址
revoke all on bdqn.* from 'dbuser'@"20.0.0.1';
#####列子####
mysql> revoke all on bdqn.* from 'dbuser'@"20.0.0.25';
Query OK,0 rows affected (0.00 sec)
mysql> show grants for 'dbuser'@'20.0.0.1;