用户篇
1. 添加用户
1.利用insert添加用户
insert into user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject,authentication_string) values ('localhost','pi',PASSWORD('pi'),'','','','');
2.刷新
flush privileges;
2. 删除普通用户
DROP USER 'pi'@'localhost'
3. root修改自己的密码
#root登录的情况下:
SET PASSWORD=PASSWORD('root')
4.root用户修改普通用户的密码
SET PASSWORD FOR 'pi'@'localhost'=PASSWORD('root');
FLUSH PRIVILEGES;
5. 普通用户修改密码
#该用户登录的情况下:
SET PASSWORD=PASSWORD('root')
权限篇
1.授权
GRANT priv_type ON database.table TO user WITH GRANT OPTION;
#priv_type 参考上面表格
#user是某个user
#赋予用户所有的权限
GRANT ALL PRIVILEGES ON pi_db.* TO 'pi'@'localhost' WITH GRANT OPTION;
#赋予pi@localhost 用户 select和update表的权限,并且可以将这些权限赋予给别的用户
GRANT SELECT,UPDATE ON pi_db TO 'pi'@'localhost' WITH GRANT OPTION;
2.收回权限
#1.收回用户所有的权限
REVOKE ALL PRIVILEGES ON database.table FROM user;
#2.收回用户SELECT的权限
REVOKE SELECT ON database.table FROM user;
3.查看用户的权限
SHOW GRANTS for 'pi'@'localhost'G
数据备份和还原
1.数据备份
#1.备份某个数据库
mysqldump -u pi -p pi_db >~/back1.sql
#2.备份某个数据库的某个表
mysqldump -u pi -p pi_db test1 >~/back2.sql
#3.备份多个数据库
mysqldump -u pi -p --databases pi_db pi_db1 >~/back2.sql
#4.备份所有数据库
mysqldump -u username -p --all-databases > back3.sql
mysqldump -u username -p dbname table1 table2 > 绝对路径/backup.sql
#5.导出文本文件
mysql -u pi -p -e "SELECT * FROM student" dbname >~/test.txt
2.数据还原
mysql -u pi -p [dbname] < backup.sql
优化
EXPLAIN SELECT 语句;