<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>船长日志 &#187; 数据库</title>
	<atom:link href="http://www.cslog.cn/tag/%e6%95%b0%e6%8d%ae%e5%ba%93/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cslog.cn</link>
	<description>最让我激动的是不知道下一个星球上能发现什么...</description>
	<lastBuildDate>Wed, 30 Jul 2025 16:06:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>读取mdf格式的sql server数据文件，并将数据以CSV格式导出</title>
		<link>http://www.cslog.cn/Content/ms-sql-server-mdf-csv/</link>
		<comments>http://www.cslog.cn/Content/ms-sql-server-mdf-csv/#comments</comments>
		<pubDate>Fri, 02 Aug 2013 15:20:35 +0000</pubDate>
		<dc:creator>船长</dc:creator>
				<category><![CDATA[信息处理]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[ms sql server]]></category>
		<category><![CDATA[数据备份]]></category>
		<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.cslog.cn/?p=1182</guid>
		<description><![CDATA[多年以前使用过微软的SQL Server 2000数据库。留下一个mdf格式的数 &#8230; <a href="http://www.cslog.cn/Content/ms-sql-server-mdf-csv/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>多年以前使用过微软的SQL Server 2000数据库。留下一个mdf格式的数据文件，临时用到想把它导出。过程真可以使用灾难来形容。现在不用使用微软的东西来工作了，真是好运啊。</p>
<p>我使用的是MS SQL Express 2005。要在Windows 7上安装，得安装SP4版本。安装后使用它附加上mdf文件。数据库就可以在上面查询了。在查询窗口输入：<br />
<code><br />
EXEC xp_cmdshell<br />
'BCP "dbname.dbo.tablename" out d:\aa.csv -t , -T -SMY-PC\SQLEXPRESS -w'<br />
</code><br />
这样的命令，就能将数据导出CSV到文件。其中-t后面的是分隔符，这里使用的是逗号，注意这样如果原来数据中也有逗号，会产生数据分隔出问题，这时可以改用其它符号（如####）。-w是指定使用unicode<a href="http://www.cslog.cn/tag/%e6%b1%89%e5%ad%97%e7%bc%96%e7%a0%81/">文字编码</a>输出。重点是要加-S参数，指定自己的PC名称\SQLEXPRESS， 不然会连接出错，错误提示如：</p>
<blockquote><p>
[SQL Native Client]Named Pipes Provider: Could not open a connection<br />
to SQL Server [2].<br />
[SQL Native Client]Login timeout expired<br />
[SQL Native Client]An error has occurred while establishing a<br />
connection to the server. When connecting to SQL Server 2005, this<br />
failure may be caused by the fact that under the default settings SQL<br />
Server does not allow remote connections.
</p></blockquote>
<div style="margin-top: 15px; font-style: italic">
<p>转载请注明: 转自<a href="http://www.cslog.cn/">船长日志</a>, 本文链接地址: <a href="http://www.cslog.cn/Content/ms-sql-server-mdf-csv/">http://www.cslog.cn/Content/ms-sql-server-mdf-csv/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/ms-sql-server-mdf-csv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql不能储存生僻汉字？试试utf8mb4字符集</title>
		<link>http://www.cslog.cn/Content/mysql-utf8mb4/</link>
		<comments>http://www.cslog.cn/Content/mysql-utf8mb4/#comments</comments>
		<pubDate>Fri, 07 Jun 2013 15:13:29 +0000</pubDate>
		<dc:creator>船长</dc:creator>
				<category><![CDATA[信息处理]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[utf8]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[汉字]]></category>
		<category><![CDATA[汉字编码]]></category>

		<guid isPermaLink="false">http://www.cslog.cn/?p=1165</guid>
		<description><![CDATA[今天发现存进mysql的数据部分丢失了。发现丢失的都是一些生僻的汉字。我这个my &#8230; <a href="http://www.cslog.cn/Content/mysql-utf8mb4/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>今天发现存进mysql的数据部分丢失了。发现丢失的都是一些生僻的汉字。我这个mysql使用的是utf8字符集，之前一直以为不会发生这种问题，今天才看到了。而且这个问题<a href="http://bbs.zdic.net/thread-115854-1-1.html">很早就存在了</a>。这是<a href="http://bugs.mysql.com/bug.php?id=14052">mysql的一个bug</a>，它的UTF-8字符集只能存储3字节字符，而有部分生僻的汉字是4个字节的，存入时就会出错：</p>
<blockquote><p>ActiveRecord::StatementInvalid: Mysql::Error: Incorrect string value: &#8216;\xF0\x90\x8D\x83\xF0\x90&#8230;&#8217; for column &#8216;content&#8217; at row 1</p></blockquote>
<p>上面的官方bug页面说要到mysql6.0才会解决。其实这个问题在mysql 5.5就解决了。5.5或以上的mysql都可以通过使用utf-16或utf8mb4等字符集解决这个问题。</p>
<p>不过使用ruby on rails的话可能<a href="http://donpark.org/blog/2013/02/16/rails-3-2-12-not-ready-for-mysql-5-5-utf8mb4">需要一些额外的设置</a>。
<div style="margin-top: 15px; font-style: italic">
<p>转载请注明: 转自<a href="http://www.cslog.cn/">船长日志</a>, 本文链接地址: <a href="http://www.cslog.cn/Content/mysql-utf8mb4/">http://www.cslog.cn/Content/mysql-utf8mb4/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/mysql-utf8mb4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>linux下使用logrotate自动备份mysql数据库</title>
		<link>http://www.cslog.cn/Content/logrotate-mysql-automated-backu/</link>
		<comments>http://www.cslog.cn/Content/logrotate-mysql-automated-backu/#comments</comments>
		<pubDate>Fri, 12 Nov 2010 15:31:19 +0000</pubDate>
		<dc:creator>船长</dc:creator>
				<category><![CDATA[站长文档]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[logrotate]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[数据备份]]></category>
		<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.cslog.cn/?p=568</guid>
		<description><![CDATA[logrotate是linux下的一个日志文件管理工具。apache,nignx &#8230; <a href="http://www.cslog.cn/Content/logrotate-mysql-automated-backu/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>logrotate是linux下的一个日志文件管理工具。apache,nignx或系统的log文件如不处理， 正常情况下会慢慢变的大， 久了， 硬盘就会爆掉。 logrotate（配合cron）就是用来周期性分割、压缩、删除日志文件用的。前段时间在找mysql数据库的自动备份方法的时候， 在 <a href="http://islandlinux.org/howto/automated-mysql-backups">http://islandlinux.org/howto/automated-mysql-backups</a> 看到了使用logrotate来备份mysql数据库的偏方， 感觉不错， 在这里和大家分享一下。</p>
<p><span id="more-568"></span></p>
<p>首先， 先创建一个脚本文件</p>
<pre>nano /usr/local/sbin/mysql_auto_backup.sh</pre>
<p>贴入如下内容</p>
<pre>#!/bin/sh
#
# 作者 Dallas Vogels 2008-10-01
#
export PATH=/bin:/usr/bin:/sbin:/usr/sbin

OUTPUTDIR="/root/mysql-backups" #将这里修改成你要放备份文件的目录
OPTIONS="--all --complete-insert --add-drop-table --extended-insert --quote-names"
CONFIG_FILE="/root/.my.cnf.backup"

# 检查备份目录是否存在
if [ ! -d $OUTPUTDIR ]; then
        mkdir $OUTPUTDIR
fi

# 获取数据库列表
DATABASES=`echo "SHOW DATABASES" | mysql --defaults-file="$CONFIG_FILE" mysql`

for DATABASE in $DATABASES; do
# 不备份 Database 和 information_schema 这两个数据库
  if [ "$DATABASE" != "Database" -a "$DATABASE" != "information_schema" ]; then
    # 数据库备份开始
    mysqldump --defaults-file="$CONFIG_FILE" $OPTIONS $DATABASE &gt; $OUTPUTDIR/$DATABASE.sql
  fi

done

exit 0</pre>
<p>将这个备份脚本添加可执行属性：</p>
<pre>sudo chmod og-rwx /usr/local/sbin/mysql_auto_backup.sh</pre>
<p>运行mysql</p>
<pre>mysql -u root -p</pre>
<p>给mysql添加专门用来备份数据库的用户：</p>
<pre>GRANT SELECT, LOCK TABLES ON *.* TO mysqlbackup@localhost IDENTIFIED BY 'password';
FLUSH PRIVILEGES;</pre>
<p>注意将password换成你想要设定的密码。</p>
<p>新创建一个mysql配置文件：</p>
<pre>nano /root/.my.cnf.backup</pre>
<p>将这个mysql备份用户信息加到里面：</p>
<pre>[client]
user="mysqlbackup"
password="password"</pre>
<p>注意再次换密码。</p>
<p>设置好这个文件的属性：</p>
<pre>chmod og-rwx /root/.my.cnf.backup</pre>
<p>现在可以测试一下数据备份脚本了，运行：</p>
<pre>/usr/local/sbin/mysql_auto_backup.sh</pre>
<p>如果可以在设定的目录（/root/mysql-backups）找到mysql数据库dump出来的sql文件，那到目前为止一切正常，继续。</p>
<p>创建用来自动备份mysql数据库的logrotate配置文件：</p>
<pre>nano /etc/logrotate.d/mysql-backups</pre>
<p>贴入如下内容:</p>
<pre>/root/mysql-backups/*.sql {
  weekly
  copy
  missingok
  rotate 30
  compress
  notifempty
  create 640 root adm
  sharedscripts
  prerotate
    /usr/local/sbin/mysql_auto_backup.sh
  endscript
}</pre>
<p>上面的设置会每周一次地将mysql中所有除Database和information_schema之外的数据库自动dump到/root/mysql-backups/目录中， 并自动将dump文件压缩成gz格式(如果不希望压缩，去掉上面的compress行）。<br />
如果希望将数据备份周期改到每天一次， 直接将上面logrotate配置文件weekly改成daily就可以了（每月一次是monthly)<br />
上面的脚本会自动保留最近30个备份（注意rotate 30参数， 修改30可设置保留的备份数）</p>
<p>最后测试一下logrotate配置文件有没有问题，强制运行logrotate：</p>
<pre>logrotate -f /etc/logrotate.d/mysql-backups</pre>
<p>这时/root/mysql-backups/目录应该出来gz格式的mysql数据库备份文件。</p>
<p>这时用logrotate的自动mysql数据库的备份部署完成了。这样每星期到/root/mysql-backups/目录下载mysql备份文件就可了。
<div style="margin-top: 15px; font-style: italic">
<p>转载请注明: 转自<a href="http://www.cslog.cn/">船长日志</a>, 本文链接地址: <a href="http://www.cslog.cn/Content/logrotate-mysql-automated-backu/">http://www.cslog.cn/Content/logrotate-mysql-automated-backu/</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.cslog.cn/Content/logrotate-mysql-automated-backu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
