MySQL 查看/备份/导入/导出/结构或数据
原创
©著作权归作者所有:来自51CTO博客作者JeesonHuang的原创作品,请联系作者获取转载授权,否则将追究法律责任
#查看某个表的结构(当前数据库mysql,查看表user):
mysql > desc user;
mysql > describe user;
mysql > show columns from user;
mysql > show fields from user;
mysql > show create table user;
mysql > select * from information_schema.columns where table_schema='mysql' and table_name='user';
----------------------------------------------------
mysqldump 备份数据
----------------------------------------------------
#导结构不导数据
> mysqldump -u root -p --opt -d 数据库名 > xxxx.sql
#栗子:
> mysqldump -u root -p --opt -d mysql > /home/kk/mysql.sql
#导数据不导结构
> mysqldump -u root -p --opt -t 数据库名 > xxxx.sql
#栗子:
> mysqldump -u root -p --opt -t mysql > /home/kk/mysql.sql
#导出数据和结构
> mysqldump -u root -p 数据库名 > xxxx.sql
#栗子:
> mysqldump -u root -p mysql > /home/kk/mysql.sql
#导出多个数据库(数据和结构 )
> mysqldump -u root -p -B 数据库A 数据库B 数据库C > xxxx.sql
#导出数据库多个表(数据和结构 )
> mysqldump -u root -p 数据库A 表A 表B > xxxx.sql
> mysqldump -u root -p 数据库A --tables 表A 表B > xxxx.sql
#栗子:
> mysqldump -u root -p mysql db user func > /home/kk/mysql.sql
> mysqldump -u root -p -B mysql --tables db func > /home/kk/mysql.sql
----------------------------------------------------
mysqldump 还原数据
----------------------------------------------------
# mysqldump -u用户 -p密码 数据库 < /home/kk/mysql.sql
#栗子:
> mysqldump -uroot -p mysql < /home/kk/mysql.sql
# mysql -u用户 -p密码 数据库 < xxxx.sql
#栗子:
> mysql -uroot -p < /home/kk/mysql.sql
> mysql -uroot -p mysql < /home/kk/mysql.sql
# 登录mysql执行
mysql> source /home/kk/mysql.sql;
压缩备份
mysqldump -uroot -p'pwd' -B db1db2 | gzip >/db_back.sql.gz
批量压缩备份
mysqldump -uroot -p'pwd' -e"show databases;" | grep -Eiv "database|infor|perfor" | sed -r 's#^([a-z].*$)#mysqldump -uroot -p 'pwd' --event -B \1|gzip > /opt/back/\1.sql.gz#g'|bash
----------------------------------------------------
outfile 导出数据
http://dev.mysql.com/doc/refman/5.7/en/select-into.html
----------------------------------------------------
mysql> select host, user from mysql.user;
# 导出数据到变量
mysql> select host, user into @x, @y from mysql.user limit 1;
mysql> select @x,@y;
# 导出数据到文件
mysql> select host, user from mysql.user into outfile '/home/kk/file_name.txt';
#可能错误
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
解决:
1. 设置安全目录: vi /etc/my.cnf
secure-file-priv=/home/kk/
2. 有权限写入目录/home/kk/
# 详细写法
select host, user
into outfile '/home/kk/file_name.txt'
fields terminated by ','
optionally enclosed by '"'
lines terminated by '\n'
from mysql.user;
select host, user from mysql.user
into outfile '/home/kk/file_name.txt'
fields terminated by ','
optionally enclosed by '"'
lines terminated by '\n';
# 其他写法
> mysql -u root -p -e "SELECT host, user FROM mysql.user" > /home/kk/file_name.txt
----------------------------------------------------
load data infile 导入数据
http://dev.mysql.com/doc/refman/5.7/en/load-data.html
----------------------------------------------------
mysql> create database test;
mysql> create table test.tab as select host,user from mysql.user where 1=2;
# 导入数据
mysql> load data infile '/home/kk/file_name.txt' into table test.tab;
# 导入数据,忽略第一行(如有标题)
mysql> load data infile '/home/kk/file_name.txt' into table test.tab ignore 1 lines;
# 自定义格式倒入(文本如: "localhost","root")
load data infile '/home/kk/file_name.txt'
into table test.tab
fields terminated by ','
optionally enclosed by '"'
lines terminated by '\n';