mysql create dabase 语法详解
由于SQL标准的存在,各个关系型数据库管理系统中创建库的语句都差不多
一、mysql 中创建数据库的语法如下:
1、创建数据库的语法:
create {database | schema } [if not exists] db_name [create_specification] -- mysql在以在创建库的时候指定库的默认字符集, create_specification: [defualt] character set [=] charset_name [default] collate [=] collation_name
二、mysql中创建数据库要有create 权限:
1、创建一个叫coder的用户
mysql:root> create user coder@'127.0.0.1' identified by '123456'; Query OK, 0 rows affected (0.00 sec)
2、coder去创建库时会报错(权限不足)
mysql:coder> show grants; +-------------------------------------------+ | Grants for coder@127.0.0.1 | +-------------------------------------------+ | GRANT USAGE ON *.* TO 'coder'@'127.0.0.1' | +-------------------------------------------+ 1 row in set (0.00 sec) mysql:coder> create database coderdba; ERROR 1044 (42000): Access denied for user 'coder'@'127.0.0.1' to database 'coderdba'
3、用root用户给coder用户加上create权限
mysql:root> grant create on *.* to coder@'127.0.0.1'; Query OK, 0 rows affected (0.00 sec)
4、coder用户重新发起连接、然后就可以创建库了
mysql -ucoder -h127.0.0.1 -P3306 -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.7.18-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial) 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:coder> create database coderdba; Query OK, 1 row affected (0.00 sec)
三、mysql数据库权限的一些吐槽:
1、mysql权限好的地方就在于它权限的层次分明
2、mysql权限不好的地方就在于它的权限粒度不是非学细、比如说上面的例子,事实上我要的是一个create database 权限
但是可惜呀它没有这么细的,它只有一个create ;但是只要在实例级别给了create 那么就是说它可以create everthing啦。
----