mysql> create database if not exists school;
创建student:
CREATE TABLE student (
id INT(10) NOT NULL UNIQUE PRIMARY KEY ,
name VARCHAR(20) NOT NULL ,
sex VARCHAR(4) ,
birth YEAR,
department VARCHAR(20) ,
address VARCHAR(50)
);
创建score表:
CREATE TABLE score (
id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT ,
stu_id INT(10) NOT NULL ,
c_name VARCHAR(20) ,
grade INT(10)
);
向student表插入记录:
INSERT INTO student VALUES( 901,‘张老大’, ‘男’,1985,‘计算机系’, ‘北京市海淀区’);
INSERT INTO student VALUES( 902,‘张老二’, ‘男’,1986,‘中文系’, ‘北京市昌平区’);
INSERT INTO student VALUES( 903,‘张三’, ‘女’,1990,‘中文系’, ‘湖南省永州市’);
INSERT INTO student VALUES( 904,‘李四’, ‘男’,1990,‘英语系’, ‘辽宁省阜新市’);
INSERT INTO student VALUES( 905,‘王五’, ‘女’,1991,‘英语系’, ‘福建省厦门市’);
INSERT INTO student VALUES( 906,‘王六’, ‘男’,1988,‘计算机系’, ‘湖南省衡阳市’);
向score表插入记录:
INSERT INTO score VALUES(NULL,901, ‘计算机’,98);
INSERT INTO score VALUES(NULL,901, ‘英语’, 80);
INSERT INTO score VALUES(NULL,902, ‘计算机’,65);
INSERT INTO score VALUES(NULL,902, ‘中文’,88);
INSERT INTO score VALUES(NULL,903, ‘中文’,95);
INSERT INTO score VALUES(NULL,904, ‘计算机’,70);
INSERT INTO score VALUES(NULL,904, ‘英语’,92);
INSERT INTO score VALUES(NULL,905, ‘英语’,94);
INSERT INTO score VALUES(NULL,906, ‘计算机’,90);
INSERT INTO score VALUES(NULL,906, ‘英语’,85);
[root@localhost mysql]# cp -pr /var/lib/mysql/school /backup
mysqldump --add-drop-table -uroot -p110119 --databases school > /backup/school.sql
#tar备份方式
tar czf /backup/school.tar.gz *
#备份前关闭mysqld程序
[root@localhost backup]# systemctl stop mysqld
#检查mysqld进程状态
[root@localhost backup]# netstat -lnupt | grep 3306
#对表的备份
[root@localhost backup]# mysql -uroot -p school student > /backup/student.$(date +%F).sql
#进入目录查看备份情况
[root@localhost backup]# ll
total 28
-rw-r--r-- 1 root root 45 Aug 5 11:23 school.tar.gz
-rw-r--r-- 1 root root 12281 Aug 5 16:59 student
-rw-r--r-- 1 root root 12281 Aug 5 17:02 student.2023-07-22.sql
mysql> create database if not exists pop;
mysql> create database if not exists mkl;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mkl |
| mysql |
| performance_schema |
| pop |
| school |
| sys |
+--------------------+
7 rows in set (0.00 sec)
[root@localhost backup]# systemctl stop mysqld
[root@localhost backup]# systemctl status mysqld
[root@localhost backup]# mysqldump -uroot -p'Redhat@123' --default-character-set=utf8 school pop mkl > backup/db.$(date +%F).sql
[root@localhost ~]# mysqldump -uroot -p'Redhat@123' -d school > /backup/t1.sql
#mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# cd /backup
[root@localhost ~]# mysql -uroot -p'Redhat@123' -B databases information_schema mkl mysql performance_schema pop school sys > test/alldb.sql
[root@localhost ~]# myloader -u root -p Redhat@123 -d test/db/ -o -B school
[root@localhost ~]# mysql -uroot -p'Redhat@123' -e 'show databases'
[root@localhost mysql]# tar tf all_db.tar.gz
innobackupex --user=root --password=Redhat@123 --defaults-file=/etc/my.cnf --
socket=/tmp/mysql.sock /server/backup/
innobackupex --copy-back /server/backup/
[root@localhost ~]# mydumper -u root -p Redhat@123 -B school -o test/school
[root@localhost test]# mysql -uroot -p'Redhat@123' -e 'drop database school'
[root@localhost test]# mysql -uroot -p'Redhat@123' -e 'show databases'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| -S |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@localhost ~]# myloader -u root -p Redhat@123 -d test/school -o -B school
[root@localhost ~]# mysql -uroot -p'Redhat@123' -e 'show databases'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| -S |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+