MySQL 查看/备份/导入/导出/结构或数据(实战)

#查看某个表的结构(当前数据库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';

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值