mysql索引、事物、存储引擎、完全、增量备份

mysql索引、事物、存储引擎、完全、增量备份

一、 mysql索引
1、 配置mysql索引
1)
创建普通索引

[[email protected] ~]# mysql -uroot [email protected]
mysql索引、事物、存储引擎、完全、增量备份
mysql> create index test_index on auth(薪资);
Query OK, 0 rows affected (0.01 sec)
2)
创建唯一索引

mysql> create unique index unique_index on auth(员工ID);
Query OK, 0 rows affected (0.01 sec)
3)
创建主键索引

mysql> create table student (姓名 char(5),编号 char(5));
Query OK, 0 rows affected (0.00 sec)
mysql> alter table student add primary key (编号);
Query OK, 0 rows affected (0.01 sec)
4)
查看auth表索引

mysql> show index from auth;
mysql索引、事物、存储引擎、完全、增量备份
5)
使用keys查询创建的索引

mysql> show keys from auth;
mysql索引、事物、存储引擎、完全、增量备份
二、 mysql事物
1、 配置mysql事物
1)
修改事物提交的方式

mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set autocommit=1;
Query OK, 0 rows affected (0.00 sec)
2)
手动运行事物处理数据

mysql> use benet;
Database changed
mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into student values (‘bob’,‘111’);
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from student;
mysql索引、事物、存储引擎、完全、增量备份
3)
设置手动回滚插入数据

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into student values (‘tom’,‘2222’);
Query OK, 1 row affected (0.01 sec)

mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from student;
mysql索引、事物、存储引擎、完全、增量备份
三、 修改mysql数据库默认引擎
1、 查看特定表使用的引擎类型
1)
查看auth表使用的引擎类型

mysql> show table status from benet where name=‘auth’;
mysql索引、事物、存储引擎、完全、增量备份
2)
查看benet库中auth表使用的mysql引擎

mysql> show create table benet.auth;
mysql索引、事物、存储引擎、完全、增量备份
3)
查看mysql数据库支持的引擎类型

mysql> mysql> show engines;
mysql索引、事物、存储引擎、完全、增量备份
4)
修改benet库中auth表引擎为MylSAM

mysql> alter table benet.auth engine=Myisam;
Query OK, 0 rows affected (0.01 sec)
mysql> show create table benet.auth;
mysql索引、事物、存储引擎、完全、增量备份
5)
修改mysql配置文件修改默认引擎

[[email protected] ~]# vim /etc/my.cnf
30 default-storage-engine=Myisam
[[email protected] ~]# systemctl restart mysqld
6)
转换benet库中auth表引擎为MylSAM

[[email protected] ~]# yum -y install perl-DBI perl-DBD-MySQL
[[email protected] ~]# mysql_convert_table_format --host=localhost --user=root [email protected] --sock=/tmp/mysql.sock --type=Myisam benet student;
四、 mysql完全备份和增量备份
1、 使用mysqldump备份恢复数据
1)
备份单个数据库数据

[[email protected] ~]# mysqldump -uroot -p autho > autho.sql
Enter password:
2)
备份多个数据库

[[email protected] ~]# mysqldump -uroot -p --databases autho benet > ./autho_benet.sql
Enter password:
3)
备份mysql中所有数据库

[[email protected] ~]# mysqldump -uroot -p --opt --all-databases > all_databases.sql
Enter password:
4)
备份benet数据库中的auth表

[[email protected] ~]# mysqldump -uroot -p benet auth > auth_user.sql
Enter password:
五、 mysql数据库数据恢复
1)
使用source恢复accp数据

mysql> create database accp;
Query OK, 1 row affected (0.00 sec)
mysql> use accp;
mysql> source ./accp.sql
2)
使用mysql命令恢复删除acco数据库

mysql> create database accp;
[[email protected] ~]# mysql -uroot -p accp < accp.sql
六、 配置增量备份
1)
开启二进制日志并重新启动

[[email protected] ~]# vim /etc/my.cnf
12 log-bin=mysql-bin
[[email protected] ~]# systemctl restart mysqld
2)
查看有没有日志

[[email protected] ~]# cd /usr/local/mysql/data/
[[email protected] data]# ls
mysql索引、事物、存储引擎、完全、增量备份
3)
查看二进制日志

[[email protected] data]# mysqlbinlog mysql-bin.000001
4)
使用二进制日志恢复数据

[[email protected] data]# cp mysql-bin.000001 /root/
[[email protected] ~]# mysqlbinlog --no-defaults ./mysql-bin.000001 | mysql -uroot -p
5)
使用起始和结束位置恢复

[[email protected] ~]# mysqlbinlog --start-position=199 --stop-position=475 ./mysql-bin.000001 | mysql -uroot -p
6)
基于起始时间和结束时间恢复数据

[[email protected] ~]# mysqlbinlog --start-datetime=‘2020-03-24 23:31:53’ --stop-datetime=‘2020-03-24 23:32:02’ ./mysql-bin.000001 | mysql -uroot -p
7)
手动日志切割

[[email protected] ~]# mysqladmin -uroot [email protected] -p flush-log