用户部分
MySQL 用户设置
如果你需要添加 MySQL 用户,你只需要在 mysql 数据库中的 user 表添加新用户即可。
你可以在创建用户时,为用户指定权限,用户权限列表如下:
权限列表:
ALTER: 修改表和索引。
CREATE: 创建数据库和表。
DELETE: 删除表中已有的记录。
DROP: 抛弃(删除)数据库和表。
INDEX: 创建或抛弃索引。
INSERT: 向表中插入新行。
REFERENCE: 未用。
SELECT: 检索表中的记录。
UPDATE: 修改现存表记录。
FILE: 读或写服务器上的文件。
PROCESS: 查看服务器中执行的线程信息或杀死线程。
RELOAD: 重载授权表或清空日志、主机缓存或表缓存。
SHUTDOWN: 关闭服务器。
ALL: 所有权限,ALL PRIVILEGES同义词。
USAGE: 特殊的 "无权限" 权限。
用 户账户包括 "username" 和 "host" 两部分,后者表示该用户被允许从何地接入。tom@'%' 表示任何地址,默认可以省略。还可以是 "tom@192.168.1.%"、"tom@%.abc.com" 等。数据库格式为 db@table,可以是 "test.*" 或 "*.*",前者表示 test 数据库的所有表,后者表示所有数据库的所有表。
子句 "WITH GRANT OPTION" 表示该用户可以为其他用户分配权限。
以下为添加用户的的实例,用户名为test,密码为test123,并授权用户可进行 SELECT, INSERT 和 UPDATE操作权限:
首先进入表:
[root@localhost mysql]# mysql -u root -pEnter password: ****** mysql> use mysql; Database changed
查看MySQL自带表中的用户信息:
mysql> select user,authentication_string,host from user; +-----------+-------------------------------------------+-----------+ | user | authentication_string | host | +-----------+-------------------------------------------+-----------+ | root | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost | | root | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | % | +-----------+-------------------------------------------+-----------+
插入用户test:
mysql> grant select,insert,update,delete,create,drop -> on tutorials.* -> to 'test'@'localhost' -> identified by 'test123'; Query OK, 0 rows affected, 1 warning (0.01 sec)
再次查看用户列表:
mysql> select user,authentication_string,host from user; +-----------+-------------------------------------------+-----------+ | user | authentication_string | host | +-----------+-------------------------------------------+-----------+ | root | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost | | root | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | % | | test | *676243218923905CF94CB52A3C9D3EB30CE8E20D | localhost | +-----------+-------------------------------------------+-----------+ 4 rows in set (0.00 sec)
删除test01用户:
mysql> select user,authentication_string,host from user; +-----------+-------------------------------------------+-----------+ | user | authentication_string | host | +-----------+-------------------------------------------+-----------+ | root | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost | | root | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | % | | test02 | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | loaclhost | | test01 | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | loaclhost | +-----------+-------------------------------------------+-----------+ 5 rows in set (0.00 sec) mysql> drop user test01@loaclhost; Query OK, 0 rows affected (0.00 sec) mysql> select user,authentication_string,host from user; +-----------+-------------------------------------------+-----------+ | user | authentication_string | host | +-----------+-------------------------------------------+-----------+ | root | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost | | root | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | % | | test02 | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | loaclhost | +-----------+-------------------------------------------+-----------+ 4 rows in set (0.00 sec)
重命名test02为test:
mysql> rename user 'test02'@'loaclhost' to 'test'@'192'; Query OK, 0 rows affected (0.00 sec) mysql> select user,authentication_string,host from user; +-----------+-------------------------------------------+-----------+ | user | authentication_string | host | +-----------+-------------------------------------------+-----------+ | root | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost | | root | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | % | | test | *6691484EA6B50DDDE1926A220DA01FA9E575C18A | 192 | +-----------+-------------------------------------------+-----------+ 4 rows in set (0.00 sec)
撤销test用户update权限:
mysql> revoke update on *.* from 'test'@'192'; Query OK, 0 rows affected (0.01 sec)
MYSQL基本查看的操作
SHOW DATABASES:
列出 MySQL 数据库管理系统的数据库列表。
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
USE 数据库名 :
选择要操作的Mysql数据库,使用该命令后所有Mysql命令都只针对该数据库。
mysql> use sys; Database changed
SHOW TABLES:
显示指定数据库的所有表,使用该命令前需要使用 use 命令来选择要操作的数据库。
mysql> show tables; +----------------+ | Tables_in_mood | +----------------+ | info | +----------------+ 1 row in set (0.00 sec)
SHOW COLUMNS FROM 数据表:
显示数据表的属性,属性类型,主键信息 ,是否为 NULL,默认值等其他信息。
mysql> show columns from info; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | char(10) | NO | | NULL | | | score | decimal(5,2) | YES | | NULL | | | hobby | int(2) | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
SHOW INDEX FROM 数据表:
显示数据表的详细索引信息,包括PRIMARY KEY(主键)。
mysql> show index from info; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | info | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 1 row in set (0.00 sec)
转载于:https://blog.51cto.com/13706064/2166243