一.mysql备份命令:
1.备份单个数据库
mysql> system mysqldump -uroot -pmybaby99 wordpress >/tmp/wordpress.sql;
mysql> system ls -l /tmp/wordpress.sql;
-rw-r--r-- 1 root root 292760 Oct 25 18:43 /tmp/wordpress.sql
2.压缩备份:
[root@ser200 mysql]# mysqldump -uroot -pmybaby99 wordpress|gzip > /server/bak/wordpress.sql.gz;
[root@ser200 mysql]# ll -l /server/bak/wordpress.sql.gz
-rw-r--r-- 1 root root 77219 Oct 25 21:21 /server/bak/wordpress.sql.gz #生产环境常用,文本压缩率高
3.导出时.加入默认字符集.方便查看SQL文件时.不会乱码.要是不加参数,并不会出导入到数据库时乱码
[root@ser200 mysql]# mysqldump -uroot -pmybaby99 --default-character-set=utf8 wordpress|gzip > /server/bak/wordpress.sql.utf8.gz;
[root@ser200 mysql]# ll /server/bak/
total 452
-rw-r--r-- 1 root root 292760 Oct 25 21:34 wordpress.sql
-rw-r--r-- 1 root root 77219 Oct 25 21:20 wordpress.sql.gz
-rw-r--r-- 1 root root 77216 Oct 25 21:36 wordpress.sql.utf8.gz
[root@ser200 mysql]# mysqldump -uroot -pmybaby99 --default-character-set=gbk wordpress|gzip > /server/bak/wordpress.sql.gbk.gz; [root@ser200 mysql]# ll /server/bak/
total 532
-rw-r--r-- 1 root root 292760 Oct 25 21:34 wordpress.sql
-rw-r--r-- 1 root root 76938 Oct 25 21:37 wordpress.sql.gbk.gz
-rw-r--r-- 1 root root 77219 Oct 25 21:20 wordpress.sql.gz
-rw-r--r-- 1 root root 77216 Oct 25 21:36 wordpress.sql.utf8.gz
4.同时备份多个数据库,生产环境中常用
[root@ser200 mysql]# mysql -uroot -pmybaby99 -B wordpress test |gzip > /server/bak/word_test.sql.gz
[root@ser200 mysql]# ll -l /server/bak/word_test.sql.gz
-rw-r--r-- 1 root root 3424 Oct 25 21:53 /server/bak/word_test.sql.gz
5.脚本中常用的 -e参数举例
[root@ser200 mysql]# mysql -uroot -pmybaby99 -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| wordpress |
+--------------------+
6.导出所有的库用-A参数
[root@ser200 mysql]# mysqldump -uroot -pmybaby99 -A |gzip >/server/bak/all.sql.gz
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
[root@ser200 mysql]# ll /server/bak/all.sql.gz
-rw-r--r-- 1 root root 297734 Oct 25 21:59 /server/bak/all.sql.gz
7.备份单个表
[root@ser200 ~]# mysqldump -uroot -pmybaby99 wordpress wp_links > /server/bak/wordpress.table.sql
[root@ser200 ~]# ll -l /server/bak/
total 836
-rw-r--r-- 1 root root 297734 Oct 25 21:59 all.sql.gz
-rw-r--r-- 1 root root 292760 Oct 25 21:34 wordpress.sql
-rw-r--r-- 1 root root 76938 Oct 25 21:37 wordpress.sql.gbk.gz
-rw-r--r-- 1 root root 77219 Oct 25 21:20 wordpress.sql.gz
-rw-r--r-- 1 root root 77216 Oct 25 21:36 wordpress.sql.utf8.gz
-rw-r--r-- 1 root root 2479 Oct 26 14:21 wordpress.table.sql
-rw-r--r-- 1 root root 3424 Oct 25 21:53 word_test.sql.g
8.备份多个表
[root@ser200 ~]# mysqldump -uroot -pmybaby99 wordpress wp_links wp_options > /server/bak/wordpress.tables.sql
[root@ser200 ~]# ll -l /server/bak/
total 1112
-rw-r--r-- 1 root root 297734 Oct 25 21:59 all.sql.gz
-rw-r--r-- 1 root root 292760 Oct 25 21:34 wordpress.sql
-rw-r--r-- 1 root root 76938 Oct 25 21:37 wordpress.sql.gbk.gz
-rw-r--r-- 1 root root 77219 Oct 25 21:20 wordpress.sql.gz
-rw-r--r-- 1 root root 77216 Oct 25 21:36 wordpress.sql.utf8.gz
-rw-r--r-- 1 root root 2479 Oct 26 14:21 wordpress.table.sql
-rw-r--r-- 1 root root 275194 Oct 26 14:23 wordpress.tables.sql
-rw-r--r-- 1 root root 3424 Oct 25 21:53 word_test.sql.gz
9.仅备份表结构,用-d参数据
[root@ser200 ~]# mysqldump -uroot -pmybaby99 -d wordpress wp_links wp_options > /server/bak/wordpress.tables.desc.sql
[root@ser200 ~]# egrep -v "^$|\*|--" /server/bak/wordpress.tables.desc.sql
DROP TABLE IF EXISTS `wp_links`;
CREATE TABLE `wp_links` (
`link_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`link_url` varchar(255) NOT NULL DEFAULT '',
`link_name` varchar(255) NOT NULL DEFAULT '',
`link_image` varchar(255) NOT NULL DEFAULT '',
`link_target` varchar(25) NOT NULL DEFAULT '',
`link_description` varchar(255) NOT NULL DEFAULT '',
`link_visible` varchar(20) NOT NULL DEFAULT 'Y',
`link_owner` bigint(20) unsigned NOT NULL DEFAULT '1',
`link_rating` int(11) NOT NULL DEFAULT '0',
`link_updated` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`link_rel` varchar(255) NOT NULL DEFAULT '',
`link_notes` mediumtext NOT NULL,
`link_rss` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`link_id`),
KEY `link_visible` (`link_visible`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `wp_options`;
CREATE TABLE `wp_options` (
`option_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`option_name` varchar(64) NOT NULL DEFAULT '',
`option_value` longtext NOT NULL,
`autoload` varchar(20) NOT NULL DEFAULT 'yes',
PRIMARY KEY (`option_id`),
UNIQUE KEY `option_name` (`option_name`)
) ENGINE=MyISAM AUTO_INCREMENT=148 DEFAULT CHARSET=utf8;
二.mysql数据库还原:
1.还原单个数据库(生产环境标准用法),也可能不用进入控制台,直接在命令行还原.下行是登陆mysql控制器的演示
mysql> system mysql -uroot -pmybaby99 test < /tmp/wordpress.sql;
mysql> show tables;
+-----------------------+
| Tables_in_test |
+-----------------------+
| wp_commentmeta |
| wp_comments |
| wp_links |
| wp_options |
| wp_postmeta |
| wp_posts |
| wp_term_relationships |
| wp_term_taxonomy |
| wp_terms |
| wp_usermeta |
| wp_users |
+-----------------------+
11 rows in set (0.01 sec
2.用source命令恢复数据库,前提得进入mysql控制台
mysql> mysql> source /server/bak/wordpress.sql
-> Query OK, 0 rows affected (0.00 sec)
3.恢复数据库时.要是以-B参数备份数据库.可以不用指定数据库
mysql -uroot -pmybaby99 > /server/bak/xxx.sql
三.mysql带e参考实现非交互式对话
1.例,查看库中的一个表
[root@ser200 ~]# mysql -uroot -pmybaby99 -e 'set names utf8;use wordpress;select * from wp_users;'
2.查看mysql状态
[root@ser200 ~]# mysql -uroot -pmybaby99 -e "show processlist;"
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 24 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
3.查看mysql变量
[root@ser200 ~]# mysql -uroot -pmybaby99 -e "show variables;"|tail -5
version_comment Source distribution
version_compile_machine x86_64
version_compile_os unknown-linux-gnu
wait_timeout 28800
warning_count 0
4.查看mysql全局状态
[root@ser200 ~]# mysql -uroot -pmybaby99 -e "show global status;"|tail -5
Threads_connected 1
Threads_created 26
Threads_running 1
Uptime 3225
Uptime_since_flush_status 3225