mysql数据库操作语句
*********************************************
Mysql中显示所有用户:
1.登录数据库
## mysql -u root -p
2.查询用户表
## SELECT User, Host, Password FROM mysql.user;
你将会看到如下这样的信息:
+------------------+--------------+--------------+
| user | host | password |
+------------------+--------------+--------------+
| root | localhost | 37as%#8123fs |
| debian-test-user | localhost | HmBEqPjC5Y |
| johnsm | localhost | |
| brian | localhost | |
| root | 111.111.111.1| |
| guest | % | |
| linuxprobe | 10.11.12.13 | RFsgY6aiVg |
+------------------+--------------+--------------+
3.显示所有的用户(不重复)
SELECT DISTINCT User FROM mysql.user;
这条命令的输出就像下面显示的这样:
+------------------+
| user |
+------------------+
| root |
| debian-test-user |
| johnsm |
| brian |
| guest |
| linuxprobe |
+------------------+
*****************************************************
1.创建数据库用户
第一种:mysql> create user 'username'@'host' identified by 'password';
Query OK, 0 rows affected (0.00 sec)
第二种:mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));
username:你将创建的用户名
host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器
例子:
CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'pig'@'192.168.1.101_' IDENDIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '';
CREATE USER 'pig'@'%';
2.授权
mysql> grant all privileges on *.* to golden@localhost with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; //刷新系统权限表
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT privileges ON databasename.tablename TO 'username'@'host'
privileges:用户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所的权限则使用ALL
databasename:数据库名
tablename:表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示,如*.*
例子:
GRANT SELECT, INSERT ON test.user TO 'pig'@'%';
GRANT ALL ON *.* TO 'pig'@'%';
注意:
用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:
mysql> GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;
3.MySQL 查看用户授予的权限
mysql> show grants for username
4.设置与更改用户密码
命令:
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
如果是当前登陆用户用:
SET PASSWORD = PASSWORD("newpassword");
例子:
SET PASSWORD FOR 'pig'@'%' = PASSWORD("123456");
5.撤销用户权限
命令:
REVOKE privilege ON databasename.tablename FROM 'username'@'host';
说明:
privilege, databasename, tablename:同授权部分
例子:
REVOKE SELECT ON *.* FROM 'pig'@'%';
注意:
假如你在给用户'pig'@'%'授权的时候是这样的(或类似的):GRANT SELECT ON test.user TO 'pig'@'%',则在使用REVOKE SELECT ON *.* FROM 'pig'@'%';命令并不能撤销该用户对test数据库中user表的SELECT 操作。相反,如果授权使用的是GRANT SELECT ON *.* TO 'pig'@'%';则REVOKE SELECT ON test.user FROM 'pig'@'%';命令也不能撤销该用户对test数据库中user表的Select权限。
具体信息可以用命令SHOW GRANTS FOR 'pig'@'%'; 查看。
6.删除用户
命令:
mysql> DROP USER 'username'@'host';
mysql> Delete FROM user Where User='test' and Host='localhost';
7. 修改指定用户密码
@>mysql -u root -p
@>密码
注意:新密码一定要放在password()中,否则修改无效。
mysql> update mysql.user set password=password('新密码') where User="test" and Host="localhost";
mysql> UPDATE user SET password=PASSWORD('输入新密码') WHERE user='root';
mysql>flush privileges;
8. 列出所有数据库
mysql>show database;
9. 切换数据库
mysql>use '数据库名';
10. 列出所有表
mysql>show tables;
11 显示数据表结构
mysql>describe 表名;
或mysql>desc 表名;
12. 查询数据库中所有表名
mysql> SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '数据库名';
13. 查询服务器中所有数据库名称
mysql> select `SCHEMA_NAME` from `information_schema`.`SCHEMATA`;
14.mysql查询表中最大自增id值
mysql> select @@IDENTITY;
15.mysql事务处理:
MyISAM不支持事务,但如果遇到数据库错误,怎么实现回滚?
MyISAM是表锁,
innodb是行锁
修改表的存储引擎,以支持事务:
mysql> alter table account_power engine= innodb;