1.用户设置
root@host# mysql -u root -p Enter password:******* mysql> use mysql; Database changed mysql> INSERT INTO user (host, user, password, select_priv, insert_priv, update_priv) VALUES (‘localhost‘, ‘guest‘, PASSWORD(‘guest123‘), ‘Y‘, ‘Y‘, ‘Y‘); Query OK, 1 row affected (0.20 sec) mysql> FLUSH PRIVILEGES; Query OK, 1 row affected (0.01 sec) mysql> SELECT host, user, password FROM user WHERE user = ‘guest‘; +-----------+---------+------------------+ | host | user | password | +-----------+---------+------------------+ | localhost | guest | 6f8c114b58f2ce9e | +-----------+---------+------------------+ 1 row in set (0.00 sec)
在添加用户时,请注意使用MySQL提供的 PASSWORD() 函数来对密码进行加密。 你可以在以上实例看到用户密码加密后为: 6f8c114b58f2ce9e.
注意:在 MySQL5.7 中 user 表的 password 已换成了authentication_string。
注意:password() 加密函数已经在 8.0.11 中移除了,可以使用 MD5() 函数代替。
注意:在注意需要执行 FLUSH PRIVILEGES 语句。 这个命令执行后会重新载入授权表。
如果你不使用该命令,你就无法使用新创建的用户来连接mysql服务器,除非你重启mysql服务器。
你可以在创建用户时,为用户指定权限,在对应的权限列中,在插入语句中设置为 ‘Y‘ 即可,用户权限列表如下:
- Select_priv
- Insert_priv
- Update_priv
- Delete_priv
- Create_priv
- Drop_priv
- Reload_priv
- Shutdown_priv
- Process_priv
- File_priv
- Grant_priv
- References_priv
- Index_priv
- Alter_priv
另外一种添加用户的方法为通过SQL的 GRANT 命令,以下命令会给指定数据库TUTORIALS添加用户 zara ,密码为 zara123 。
root@host# mysql -u root -p Enter password:******* mysql> use mysql; Database changed mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP -> ON TUTORIALS.* -> TO ‘zara‘@‘localhost‘ -> IDENTIFIED BY ‘zara123‘;
2.SHOW COLUMNS FROM 数据表:
显示数据表的属性,属性类型,主键信息 ,是否为 NULL,默认值等其他信息。
mysql> SHOW COLUMNS FROM runoob_tbl; +-----------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+--------------+------+-----+---------+-------+ | runoob_id | int(11) | NO | PRI | NULL | | | runoob_title | varchar(255) | YES | | NULL | | | runoob_author | varchar(255) | YES | | NULL | | | submission_date | date | YES | | NULL | | +-----------------+--------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)
3.SHOW INDEX FROM 数据表:
显示数据表的详细索引信息,包括PRIMARY KEY(主键)。
mysql> SHOW INDEX FROM runoob_tbl; +------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | runoob_tbl | 0 | PRIMARY | 1 | runoob_id | A | 2 | NULL | NULL | | BTREE | | | +------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 1 row in set (0.00 sec)
4.SHOW TABLE STATUS LIKE [FROM db_name] [LIKE ‘pattern‘] \G:
该命令将输出Mysql数据库管理系统的性能及统计信息。
mysql> SHOW TABLE STATUS FROM RUNOOB; # 显示数据库 RUNOOB 中所有表的信息 mysql> SHOW TABLE STATUS from RUNOOB LIKE ‘runoob%‘; # 表名以runoob开头的表的信息 mysql> SHOW TABLE STATUS from RUNOOB LIKE ‘runoob%‘\G; # 加上 \G,查询结果按列打印