数据库--问题1--mysql常用的命令,用户及权限管理

数据库--问题1--mysql常用的命令


1.SQL

(1)DDL 数据定义语言      create  drop  alter  show(增删改查)

(2)DML 数据操作             insert  delete  update  select(增删改查)

(3)DCL 数据控制             grant  revoke   (分权限,回收权限)

2.MySQL

(1)存储引擎 

(2)索引

(3)事务

(4)锁机制

(5)存储过程和触发器

进入MySQL前的操作:(前提是已经安装过mysql)

(1)打开LINUX终端

(2)输入命令:su

(3)然后输入自己设置的密码

(4)输入命令(mysqld 为可执行文件名称):service mysqld start

(5)输入命令(p代表密码):mysql -u root -p

(6)如果没有设置密码,直接回车

成功进入mysql后,最后一行会显示:mysql>

[jingjing@localhost Desktop]$ su
Password: 
[root@localhost Desktop]# service mysqld start
Starting mysqld:                                           [  OK  ]
[root@localhost Desktop]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.61 Source distribution

Copyright (c) 2000, 2011, 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> 

进入数据库后,查看数据库:

(1)显示有哪些数据库,输入命令:show databases;

(2)选定数据库,输入命令:use sql;(sql为你自己要选定的数据库的名字)

(3)显示这个数据库的表,输入命令:show tables;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| CY1211             |
| Parking            |
| intelligent_park   |
| mysql              |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> use Parking;
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> show tables;
+-------------------+
| Tables_in_Parking |
+-------------------+
| dingdan_car       |
+-------------------+
1 row in set (0.00 sec)

mysql> 

MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用。如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接。

在 MySQL 命令行模式下输入如下命令可以为 MySQL 创建一个新用户:

1

CREATE USER username IDENTIFIED BY 'password' ;

新用户创建完成,但是此刻如果以此用户登陆的话,会报错,因为我们还没有为这个用户分配相应权限,分配权限的命令如下:

1

GRANT ALL PRIVILEGES ON *.* TO 'username' 'localhost' IDENTIFIED BY 'password' ;

授予username用户在所有数据库上的所有权限。

如果此时发现刚刚给的权限太大了,如果我们只是想授予它在某个数据库上的权限,那么需要切换到root 用户撤销刚才的权限,重新授权:

1

2

REVOKE ALL PRIVILEGES ON *.* FROM 'username' 'localhost' ;

GRANT ALL PRIVILEGES ON wordpress.* TO 'username' 'localhost' IDENTIFIED BY 'password' ;

甚至还可以指定该用户只能执行 select 和 update 命令:

1

GRANT SELECT UPDATE ON wordpress.* TO 'username' 'localhost' IDENTIFIED BY 'password' ;

这样一来,再次以username登陆 MySQL,只有wordpress数据库是对其可见的,并且如果你只授权它select权限,那么它就不能执行delete 语句。

另外每当调整权限后,通常需要执行以下语句刷新权限:

1

FLUSH PRIVILEGES ;

删除刚才创建的用户:

1

DROP USER username@localhost;

仔细上面几个命令,可以发现不管是授权,还是撤销授权,都要指定响应的host(即 @ 符号后面的内容),因为以上及格命令实际上都是在操作mysql 数据库中的user表,可以用如下命令查看相应用户及对应的host:

1

SELECT User , Host FROM user ;

实际操作上面的命令如下:我的新用户名是:yonggan    密码是:yonggan    ,我本身是有数据库的,如果有问题的话,有可能是你没有选择数据库。

感兴趣的话,可以查一查workpress,

在创建的时候,如果出错,先好好检查命令是否输入错误,然后密码上一定要有单引号,出现其他错误记得去查一下,不要太着急,多踩坑,以后才能避雷

mysql> create user yonggan identified by 'yonggan';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to 'yonggan'@'localhost' identified by 'yonggan';
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for yonggan@localhost;
+-------------------------------------------------------------------------------------------------------------------------+
| Grants for yonggan@localhost                                                                                            |
+-------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'yonggan'@'localhost' IDENTIFIED BY PASSWORD '*4CB72E146ABF8859212AE815F64D98B2484511FA' |
+-------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> REVOKE ALL privileges on *.* from 'yonggan'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on Parking.* to yonggan@localhost identified by 'yonggan';
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for yonggan@localhost;+----------------------------------------------------------------------------------------------------------------+
| Grants for yonggan@localhost                                                                                   |
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'yonggan'@'localhost' IDENTIFIED BY PASSWORD '*4CB72E146ABF8859212AE815F64D98B2484511FA' |
| GRANT ALL PRIVILEGES ON `Parking`.* TO 'yonggan'@'localhost'                                                   |
+----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> grant select,update,delete on workpress.* to yonggan@localhost identified by 'yonggan';
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for yonggan@localhost;+----------------------------------------------------------------------------------------------------------------+
| Grants for yonggan@localhost                                                                                   |
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'yonggan'@'localhost' IDENTIFIED BY PASSWORD '*4CB72E146ABF8859212AE815F64D98B2484511FA' |
| GRANT ALL PRIVILEGES ON `Parking`.* TO 'yonggan'@'localhost'                                                   |
| GRANT SELECT, UPDATE, DELETE ON `workpress`.* TO 'yonggan'@'localhost'                                         |
+----------------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

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

mysql> show grants for yonggan@localhost;
ERROR 1141 (42000): There is no such grant defined for user 'yonggan' on host 'localhost'
mysql> 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值