备份数据库(myqldump)
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mingongge | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.08 sec) mysql> create database a; Query OK, 1 row affected (0.00 sec) mysql> use a; Database changed mysql> create table a1(name char(20),age int(10)); Query OK, 0 rows affected (0.07 sec) mysql> insert into a1(name,age) values('lishi',19); Query OK, 1 row affected (0.06 sec) mysql> select * from a1; +-------+------+ | name | age | +-------+------+ | lishi | 19 | +-------+------+ 1 row in set (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | a | | mingongge | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec)
备份某一个库。
[root@localhost ~]# mysqldump -uroot -p123 a > 1.sql mysqldump: [Warning] Using a password on the command line interface can be insecure. [root@localhost ~]# ls 1.sql -l -rw-r--r-- 1 root root 1798 1月 16 09:55 1.sql
恢复到a2库。
[root@localhost ~]# mysql -uroot -p123 a2 < 1.sql mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1049 (42000): Unknown database 'a2' a2库必须事先存在 mysql> create database a2; Query OK, 1 row affected (0.00 sec) mysql> exit Bye [root@localhost ~]# mysql -uroot -p123 a2 < 1.sql mysql: [Warning] Using a password on the command line interface can be insecure. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | a | | a2 | | mingongge | | mysql | | performance_schema | | sys | +--------------------+ 7 rows in set (0.00 sec) mysql> use a2; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +--------------+ | Tables_in_a2 | +--------------+ | a1 | +--------------+ 1 row in set (0.00 sec) mysql> select * from a1; +-------+------+ | name | age | +-------+------+ | lishi | 19 | +-------+------+ 1 row in set (0.00 sec)
备份其中的一个表
mysql> create database b; Query OK, 1 row affected (0.00 sec) mysql> use b Database changed mysql> create table b1(name char(20),age int(10)); Query OK, 0 rows affected (0.06 sec) [root@localhost ~]# mysqldump -uroot -p123 b b1 > 4.sql mysqldump: [Warning] Using a password on the command line interface can be insecure. [root@localhost ~]# ls 4.sql 4.sql
还原一张表
[root@localhost ~]# mysql -uroot -p123 yu < 4.sql mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1049 (42000): Unknown database 'yu' mysql> create database yu; Query OK, 1 row affected (0.00 sec) [root@localhost ~]# mysql -uroot -p123 yu < 4.sql mysql: [Warning] Using a password on the command line interface can be insecure. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | a | | a2 | | b | | mingongge | | mysql | | performance_schema | | sys | | yu | +--------------------+ 9 rows in set (0.00 sec) mysql> show tables; +--------------+ | Tables_in_yu | +--------------+ | b1 | +--------------+ 1 row in set (0.00 sec)
同时备份多个数据库
[root@localhost ~]# mysqldump -uroot -p123 --databases a b > 10.sql mysqldump: [Warning] Using a password on the command line interface can be insecure. [root@localhost ~]# ls 10.sql -l -rw-r--r-- 1 root root 2576 1月 16 10:15 10.sql
恢复。
[root@localhost ~]# mysql -uroot -p123 < 10.sql mysql: [Warning] Using a password on the command line interface can be insecure.
备份
[root@localhost ~]# mysqldump -uroot -p -B a b mysql|gzip >/root/a_mysql_$(date +%F).sql.gz Enter password: [root@localhost ~]# ls -l a_mysql_2020-01-16.sql.gz -rw-r--r-- 1 root root 228989 1月 16 10:23 a_mysql_2020-01-16.sql.gz [root@localhost ~]# mysqldump -uroot -p -B --events a b mysql|gzip >/root/a_mysql0_$(date +%F).sql02.gz Enter password: [root@localhost ~]# ls -l a_mysql* -rw-r--r-- 1 root root 229027 1月 16 10:25 a_mysql0_2020-01-16.sql02.gz -rw-r--r-- 1 root root 228989 1月 16 10:23 a_mysql_2020-01-16.sql.gz
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?