1.Mysql管理指令

1.创建登录用户
  1. MariaDB [mysql]> create user user1@'192.168.%.%' identified by 'user1';
  2. Query OK, 0 rows affected (0.00 sec)
  3. #创建user账户,允许192.168.0.0网段登录,密码为user1;‘%’为通配符;

2.给自己的用户改密码
  1. MariaDB [mysql]> set password=password('123456');

3.给其他用户修改密码
  1. MariaDB [mysql]> set password for user1@'192.168.%.%'=password('111');

4. 找回root密码
  1. [root@localhost ~]# vim /etc/my.cnf #修改mysql配置文件
  2. skip-grant-tables #跳过密码检查
  3. [root@localhost ~]# systemctl restart mariadb
  4. [root@localhost ~]# mysql
  5. MariaDB [(none)]> update mysql.user set password=password('123456') where user='root';

5. 给用户授权
  1. 1.普通用户user1登录时只能查看information表和test
  2. [root@localhost ~]# mysql -u user1 -h 192.168.10.11 -p 111 #user1登录mysql
  3. MariaDB [(none)]> show databases; #user1只能查看这两个表
  4. +--------------------+
  5. | Database |
  6. +--------------------+
  7. | information_schema |
  8. | test |
  9. +--------------------+
  10. 2 rows in set (0.00 sec)
  11. 2.root下修改user1权限,使它能够查看user
  12. [root@localhost ~]# mysql -u root -p123456
  13. MariaDB [(none)]> grant select on mysql.user to user1@'192.168.%.%'; #允许user1查询mysql下的user表
  14. MariaDB [mysql]> flush privileges; #刷新权限
  15. 3.使用user1登录,查看结果
  16. MariaDB [(none)]> show databases;
  17. +--------------------+
  18. | Database |
  19. +--------------------+
  20. | information_schema |
  21. | mysql |
  22. | test |
  23. +--------------------+
  24. 可以授予的权限类型:
  25. allselectdeletdropupdate

6. 取消用户权限
  1. 1.创建user1,并授权对mysql数据库的user表有selectinsert权限
  2. MariaDB [(none)]> grant select,insert on mysql.user to user1@'192.168.%.%' identified by 'user1';
  3. MariaDB [(none)]> flush privileges;
  4. 2.使用user1登录,查看数据库
  5. [root@localhost mysql]# mysql -uuser1 -puser1 -h 192.168.10.11
  6. MariaDB [(none)]> show grants for user1@'192.168.%.%'; #查看用户权限
  7. +-------------------------------------------------------------------------------------------------------+
  8. | Grants for user1@192.168.%.% |
  9. +-------------------------------------------------------------------------------------------------------+
  10. | GRANT USAGE ON *.* TO 'user1'@'192.168.%.%' IDENTIFIED BY PASSWORD '*34D356465421354asoasdasd468C4' |
  11. | GRANT ALL PRIVILEGES ON `mysql`.`user` TO 'user1'@'192.168.%.%' |
  12. +-------------------------------------------------------------------------------------------------------+
  13. MariaDB [(none)]> show databases; #可以查看mysql库
  14. +--------------------+
  15. | Database |
  16. +--------------------+
  17. | information_schema |
  18. | mysql |
  19. | test |
  20. +--------------------+
  21. 3.取消查看权限
  22. [root@localhost ~]# mysql -uroot -p123456
  23. MariaDB [(none)]> revoke select on mysql.user from user1@'192.168.%.%';
  24. 4.登录user1查看
  25. MariaDB [mysql]> show databases;
  26. +--------------------+
  27. | Database |
  28. +--------------------+
  29. | information_schema |
  30. | test |
  31. +--------------------+
  32. MariaDB [mysql]> show grants for user1@'192.168.%.%';
  33. +-------------------------------------------------------------------------------------------------------+
  34. | Grants for user1@192.168.%.% |
  35. +-------------------------------------------------------------------------------------------------------+
  36. | GRANT USAGE ON *.* TO 'user1'@'192.168.%.%' IDENTIFIED BY PASSWORD '*34D37F0D1D71C3DBF28E291705468C4' |
  37. --------------------------------------------------------------------------------------------------------+
  38. #登录地址不同,权限不一样,user1@192.168.%.%和user1@localhost的权限是不一样的

2.SQL语句

1.创建数据库
  1. MariaDB [mysql]> create databse web

2. 切换数据库
  1. MariaDB [mysql]> use web

3. 创建数据表
  1. MariaDB [web]> create table stu1( id int primary key auto_increment,
  2. name char(20) not null,
  3. age int not null,
  4. sex char(1) not null default 'M' );
  5. 约束:
  6. primary key:主键约束,唯一非空;
  7. auto_increment:自增长;
  8. not null:非空;
  9. default:默认值
  10. foreign key:外键;

4.查看表字段
  1. MariaDB [web]> desc stu1;
  2. +-------+----------+------+-----+---------+----------------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +-------+----------+------+-----+---------+----------------+
  5. | id | int(11) | NO | PRI | NULL | auto_increment |
  6. | name | char(20) | NO | | NULL | |
  7. | age | int(11) | NO | | NULL | |
  8. | sex | char(1) | NO | | M | |
  9. +-------+----------+------+-----+---------+----------------+

5.插入数据
  1. 1.按字段顺序插入
  2. MariaDB [web]> insert into stu1 values (1,'zhangsan','18','M');
  3. MariaDB [web]> select * from stu1;
  4. +----+----------+-----+-----+
  5. | id | name | age | sex |
  6. +----+----------+-----+-----+
  7. | 1 | zhangsan | 18 | M |
  8. +----+----------+-----+-----+
  9. 2.选择字段插入
  10. MariaDB [web]> insert into stu1 (name,age) values ('lisi','20');
  11. MariaDB [web]> select * from stu1;
  12. +----+----------+-----+-----+
  13. | id | name | age | sex |
  14. +----+----------+-----+-----+
  15. | 1 | zhangsan | 18 | M |
  16. | 2 | lisi | 20 | M |
  17. +----+----------+-----+-----+
  18. #这里id是自增长的,sex是有默认值;
  19. 注意:非空和主键的字段不插入值,会报错;
  20. 3.将一个表的数据复制到另一个表
  21. MariaDB [web]> insert into stu2 (id,name) select id,name from stu1;
  22. MariaDB [web]> select id,name from stu2;
  23. +----+----------+
  24. | id | name |
  25. +----+----------+
  26. | 1 | zhangsan |
  27. | 2 | lisi |
  28. +----+----------+

6.删除数据
  1. MariaDB [web]> select * from stu1
  2. +----+----------+-----+-----+
  3. | id | name | age | sex |
  4. +----+----------+-----+-----+
  5. | 1 | zhangsan | 18 | M |
  6. | 2 | lisi | 20 | M |
  7. | 3 | wangwu | 19 | M |
  8. | 4 | zhaoliu | 50 | M |
  9. +----+----------+-----+-----+
  10. MariaDB [web]> delete from stu1 where age>20; #删除年龄大于20的记录
  11. MariaDB [web]> select * from stu1;
  12. +----+----------+-----+-----+
  13. | id | name | age | sex |
  14. +----+----------+-----+-----+
  15. | 1 | zhangsan | 18 | M |
  16. | 2 | lisi | 20 | M |
  17. | 3 | wangwu | 19 | M |
  18. +----+----------+-----+-----+

7. 修改表中的数据
  1. MariaDB [web]> update stu1 set sex='W' where id=2; #将id=2的sex修改为w
  2. MariaDB [web]> select * from stu1;
  3. +----+----------+-----+-----+
  4. | id | name | age | sex |
  5. +----+----------+-----+-----+
  6. | 1 | zhangsan | 18 | M |
  7. | 2 | lisi | 20 | W |
  8. | 3 | wangwu | 19 | M |
  9. +----+----------+-----+-----+

8. 数据表字段修改删除增加
  1. 1.增加表的字段
  2. MariaDB [web]> alter table stu1 add address char(50); #添加address字段,类型char(50),默认插入在表的最后
  3. MariaDB [web]> alter table stu1 add phone int after name; #在name字段之后添加phone字段
  4. 2.修改字段属性
  5. MariaDB [web]> alter table stu1 modify name char(10); #modify只能修改字段的数据类型
  6. MariaDB [web]> alter table stu1 change name username char(8) null; #change可以修改整个字段,包括字段名

3.mysql备份

1. 使用mysqldump指令备份
  1. [root@localhost ~]# mysqldump -u root -p123123 web stu1 > /tmp/stu1.sql
  2. [root@localhost ~]# mysql -uroot -p123123
  3. MariaDB [(none)]> use web
  4. MariaDB [web]> drop tables stu1;
  5. MariaDB [web]> show tables;
  6. Empty set (0.00 sec)
  7. [root@localhost ~]# mysql -uroot -p123123 web < /tmp/stu1.sql
  8. [root@localhost ~]# mysql -uroot -p123123
  9. MariaDB [(none)]> select * from web.stu1;
  10. +----+----------+-----+-----+
  11. | id | name | age | sex |
  12. +----+----------+-----+-----+
  13. | 1 | zhangsan | 18 | M |
  14. | 2 | lisi | 20 | M |
  15. +----+----------+-----+-----+

2. mysql-binlog日志备份
  1. 1.修改my.cnf配置文件,开启binlog日志记录功能
  2. [root@localhost ~]# vim /etc/my.cnf
  3. log-bin=mysq-bin
  4. [root@localhost ~]# systemctl restart mariadb
  5. 2.删除创建的表
  6. [root@localhost mysql]# mysql -uroot -p123123
  7. MariaDB [(none)]> use web
  8. MariaDB [web]> drop tables stu1;
  9. 3.使用mysqlbinlog恢复
  10. [root@localhost mysql]# mysqlbinlog --start-datetime '2020-04-13 11:05:45' --stop-datetime '2020-04-13 11:05:46' /var/lib/mysql/mysq-bin.000001 | mysql -uroot -p123123
  11. 4.验证
  12. MariaDB [(none)]> select * from web.stu1;
  13. +----+----------+-----+-----+
  14. | id | name | age | sex |
  15. +----+----------+-----+-----+
  16. | 1 | zhangsan | 18 | M |
  17. | 2 | lisi | 20 | M |
  18. +----+----------+-----+-----+