mysql 数据库相关操作

mysql:

status 可以查看当前连接的信息

show create table xxx 可以查看表的创建命令

show create database xxx 可以查看数据库的创建命令


mysqldump:

mysqldump -uuser -ppassword --single-transaction --complete-insert DB table --where="XXXXX" 


只导数据,不导结构 -t

只导结构,不导数据 -d


--default-character-set=latin1 设置导出的字符编码, 设置set为数据库表的编码格式,不然会出现乱码


这里有更多详细的:http://jeffxie.blog.51cto.com/1365360/302562


数据库备份和导入

可以使用mysqldump(mysql导入),或者load data infile和(select * from xxx into filename), load方式比mysqldump要快

究其根源主要是MySQL内部对于load 和 insert的处理机制不同.
Load的处理机制是:在执行load之前,会关掉索引,当load全部执行完成后,再重新创建索引.
Insert的处理机制是:每插入一条则更新一次数据库,更新一次索引.
补充:另外,load与insert的不同还体现在load省去了sql语句解析,sql引擎处理,而是直接生成文件数据块,所以会比Insert快很多.

引用http://www.itpub.net/thread-510339-1-1.html


另外mysqldump也可以产生load格式的数据文件,还会同时生成创建表格的sql,具体看这里:http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_tab

  •  --tab=path-T path

    Produce tab-separated text-format data files. For each dumped table, mysqldump creates a tbl_name.sql file that contains the CREATE TABLE statement that creates the table, and the server writes a tbl_name.txt file that contains its data. The option value is the directory in which to write the files.

    Note

    This option should be used only when mysqldump is run on the same machine as the mysqld server. You must have the FILE privilege, and the server must have permission to write files in the directory that you specify.

    By default, the .txt data files are formatted using tab characters between column values and a newline at the end of each line. The format can be specified explicitly using the --fields-xxx and --lines-terminated-byoptions.

    As of MySQL 5.1.38, column values are converted to the character set specified by the --default-character-set option. Prior to 5.1.38 or if no such option is present, values are dumped using the binary character set. In effect, there is no character set conversion. If a table contains columns in several character sets, the output data file will as well and you may not be able to reload the file correctly.


默认mysqldump导出一行是有很多values的,如果要一条记录只导出一行,只需要加--extented-insert=false


mysql字符集解释,(乱码)

这里有一篇文章讲的算是很详细:http://www.laruence.com/2008/01/05/12.html

总的来说,mysql的字符串字符集,有4个地方是相关的。

1.character_set_client,指明客户端的数据编码,(客户端的实际数据可以跟这个不一样)

2.character_set_connection,指明连接的数据编码

3.数据库数据存储 这个是数据库本身存储的编码,它的来源列编码》表编码》数据库编码》mysql服务器编码

4.character_set_results, 指明返回给客户端的数据编码格式

当这4种编码设置不一致时,mysql会将相应的过程进行转编码,比如1是utf8,2是gbk,那么由1到2,将进行utf8到gbk的编码转换

当一致时不会进行编码转换,比如使用set names latin1(这个会设置1,2,4的编码为latin1),并且3的编码也是latin1的话,那么即使数据最初始是utf8,那么在数据从客户端保存到数据库的过程中,不会进行任何编码转换,最后数据会以utf8的形式存储在数据库中(虽然数据库表是latin1),读出的时候也不会转换,会以utf8的编码给客户端(虽然指明的是latin1,但是因为mysql发现4和3是一致的,它不会知道数据其实本身是utf8编码的,所以它不会进行编码转换,就直接把数据(utf8,数据本身的编码)给到客户端)



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值