简析mysql字符集导致恢复数据库报错问题

 更新时间:2017年10月13日 11:07:48   投稿:mengwei  
这篇文章主要介绍了简析mysql字符集导致恢复数据库报错问题,具有一定参考价值,需要的朋友可以了解。

mysql字符集编码错误的导入数据会提示错误了,这个和插入数据一样如果保存的数据与mysql编码不一样那么肯定会出现导入乱码或插入数据丢失的问题,下面我们一起来看一个例子。

<script>ec(2);</script>

恢复数据库报错:由于字符集问题,最原始的数据库默认编码是latin1,新备份的数据库的编码是utf8,因此导致恢复错误。

[root@hk byrd]# /usr/local/mysql/bin/mysql -uroot -p'admin' t4x < /tmp/11x-B-2014-06-18.sql 
ERROR 1064 (42000) at line 292: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''[caption id=\"attachment_271\" align=\"aligncenter\" width=\"300\"]<a href=\"ht' at line 1

修复方法(未实测):

[root@Test ~]# /usr/local/mysql/bin/mysql -uroot -p'admin' --default-character-set=latin1 t4x < /tmp/11x-B-2014-06-18.sql 
MySQL
-- MySQL dump 10.13 Distrib 5.5.37, for Linux (x86_64)
--
-- Host: localhost  Database: t4x
-- ------------------------------------------------------
-- Server version    5.5.37-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE=' 00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `t4x`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `t4x` /*!40100 DEFAULT CHARACTER SET utf8 */;
--
-- Table structure for table `wp_baidusubmit_sitemap`
--
DROP TABLE IF EXISTS `wp_baidusubmit_sitemap`;
/*!40101 SET @saved_cs_client   = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `wp_baidusubmit_sitemap` (
 `sid` int(11) NOT NULL AUTO_INCREMENT,
 `url` varchar(255) NOT NULL DEFAULT '',
 `type` tinyint(4) NOT NULL,
 `create_time` int(10) NOT NULL DEFAULT '0',
 `start` int(11) DEFAULT '0',
 `end` int(11) DEFAULT '0',
 `item_count` int(10) unsigned DEFAULT '0',
 `file_size` int(10) unsigned DEFAULT '0',
 `lost_time` int(10) unsigned DEFAULT '0',
 PRIMARY KEY (`sid`),
 KEY `start` (`start`),
 KEY `end` (`end`)
) ENGINE=MyISAM AUTO_INCREMENT=84 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
0
1
[root@hk byrd]# /usr/local/mysql/bin/mysql -uroot -p'admin' t4x < /tmp/t4x-B-2014-06-17.sql 
ERROR 1064 (42000) at line 295: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''i' at line 1

MySQL

-- MySQL dump 10.11
--
-- Host: localhost  Database: t4x
-- ------------------------------------------------------
-- Server version    5.0.95-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE=' 00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
-- 
-- Current Database: `t4x`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `t4x` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `t4x`;
--
-- Table structure for table `wp_baidusubmit_sitemap`
--
DROP TABLE IF EXISTS `wp_baidusubmit_sitemap`;
/*!40101 SET @saved_cs_client   = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `wp_baidusubmit_sitemap` (
 `sid` int(11) NOT NULL auto_increment,
 `url` varchar(255) NOT NULL default '',
 `type` tinyint(4) NOT NULL,
 `create_time` int(10) NOT NULL default '0',
 `start` int(11) default '0',
 `end` int(11) default '0',
 `item_count` int(10) unsigned default '0',
 `file_size` int(10) unsigned default '0',
 `lost_time` int(10) unsigned default '0',
 PRIMARY KEY (`sid`),
 KEY `start` (`start`),
 KEY `end` (`end`)
) ENGINE=MyISAM AUTO_INCREMENT=83 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

字符集相关:

MySQL

mysql>show variables like '%character_set%';
-------------------------- ---------------------------- 
| Variable_name      | Value           |
-------------------------- ---------------------------- 
| character_set_client   | utf8            |
| character_set_connection | utf8            |
| character_set_database  | utf8            |
| character_set_filesystem | binary           |
| character_set_results  | utf8            |
| character_set_server   | latin1           |
| character_set_system   | utf8            |
| character_sets_dir    | /usr/share/mysql/charsets/ |
-------------------------- ---------------------------- 
mysql>set names gbk;
mysql>show variables like '%character_set%';
-------------------------- ---------------------------- 
| Variable_name      | Value           |
-------------------------- ---------------------------- 
| character_set_client   | gbk            |
| character_set_connection | gbk            |
| character_set_database  | utf8            |
| character_set_filesystem | binary           |
| character_set_results  | gbk            |
| character_set_server   | latin1           |
| character_set_system   | utf8            |
| character_sets_dir    | /usr/share/mysql/charsets/ |
-------------------------- ---------------------------- 
mysql>system cat /etc/my.cnf | grep default  #客户端设置字符集client下面 
default-character-set=gbk
mysql>show variables like '%character_set%';
-------------------------- ---------------------------- 
| Variable_name      | Value           |
-------------------------- ---------------------------- 
| character_set_client   | gbk            |
| character_set_connection | gbk            |
| character_set_database  | latin1           |
| character_set_filesystem | binary           |
| character_set_results  | gbk            |
| character_set_server   | latin1           |
| character_set_system   | utf8            |
| character_sets_dir    | /usr/share/mysql/charsets/ |
-------------------------- ---------------------------- 
mysql> system cat /etc/my.cnf|grep character-set-server  #客户端设置字符集mysqld下面 
character-set-server = cp1250 
mysql> show variables like '%character_set%';
-------------------------- -------------------------------------------- 
| Variable_name      | Value                   |
-------------------------- -------------------------------------------- 
| character_set_client   | utf8                    |
| character_set_connection | utf8                    |
| character_set_database  | cp1250                   |
| character_set_filesystem | binary                   |
| character_set_results  | utf8                    |
| character_set_server   | cp1250                   |
| character_set_system   | utf8                    |
| character_sets_dir    | /byrd/service/mysql/5.6.26/share/charsets/ |
-------------------------- -------------------------------------------- 
8 rows in set (0.00 sec)

其他的一些设置方法:

修改数据库的字符集

  mysql>use mydb
  mysql>alter database mydb character set utf-8;

创建数据库指定数据库的字符集

  mysql>create database mydb character set utf-8;

通过配置文件修改:

修改/var/lib/mysql/mydb/db.opt

default-character-set=latin1
default-collation=latin1_swedish_ci


default-character-set=utf8
default-collation=utf8_general_ci

重起MySQL:

[root@bogon ~]# /etc/rc.d/init.d/mysql restart

通过MySQL命令行修改:

mysql> set character_set_client=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_connection=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_database=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_results=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_server=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_system=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> set collation_connection=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> set collation_database=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> set collation_server=utf8;
Query OK, 0 rows affected (0.01 sec)

查看:

mysql> show variables like 'character_set_%';
-------------------------- ---------------------------- 
| Variable_name       | Value            |
-------------------------- ---------------------------- 
| character_set_client   | utf8            |
| character_set_connection | utf8            |
| character_set_database  | utf8            |
| character_set_filesystem | binary           |
| character_set_results   | utf8            |
| character_set_server   | utf8            |
| character_set_system   | utf8            |
| character_sets_dir    | /usr/share/mysql/charsets/ |
-------------------------- ---------------------------- 
8 rows in set (0.03 sec)
mysql> show variables like 'collation_%';
---------------------- ----------------- 
| Variable_name     | Value      |
---------------------- ----------------- 
| collation_connection | utf8_general_ci |
| collation_database  | utf8_general_ci |
| collation_server   | utf8_general_ci |
---------------------- ----------------- 
3 rows in set (0.04 sec)

总结

以上就是本文关于简析mysql字符集导致恢复数据库报错问题的全部内容,希望对大家有所帮助。有什么问题可以随时留言,小编会及时回复大家。感谢朋友们对本站的支持!

相关文章

  • 修改MySQL数据库中表和表中字段的编码方式的方法

    修改MySQL数据库中表和表中字段的编码方式的方法

    这篇文章主要介绍了如何修改MySQL数据库中表和表中字段的编码方式,需要的朋友可以参考下
    2014-05-05
  • MySQL学习之分组查询的用法详解

    MySQL学习之分组查询的用法详解

    这篇文章主要为大家详细介绍一下MySQL中分组查询的使用,文中的示例代码讲解详细,对我们学习MySQL有一定帮助,需要的可以参考一下
    2022-07-07
  • mysql存储过程之返回多个值的方法示例

    mysql存储过程之返回多个值的方法示例

    这篇文章主要介绍了mysql存储过程之返回多个值的方法,结合实例形式分析了mysql存储过程返回多个值的实现方法与PHP调用技巧,需要的朋友可以参考下
    2019-12-12
  • MySQL安装配置方法教程

    MySQL安装配置方法教程

    这篇文章主要为大家详细分享了MySQL安装配置方法教程,包括Linux/UNIX和Window系统上安装MySQL的具体步骤,感兴趣的朋友可以参考一下
    2016-05-05
  • MySQL外键约束的删除和更新总结

    MySQL外键约束的删除和更新总结

    这篇文章主要给大家总结MySQL外键约束的删除和更新,文中通过代码示例和图文介绍的非常详细,对大家了解MySQL外键约束有一定的帮助,需要的朋友可以参考下
    2024-02-02
  • MySQL中ON DUPLICATE key update的使用

    MySQL中ON DUPLICATE key update的使用

    本文主要介绍了MySQL中ON DUPLICATE key update的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • mysql间隙锁加锁11个规则(案例分析)

    mysql间隙锁加锁11个规则(案例分析)

    这篇文章主要介绍了mysql间隙锁加锁11个规则 ,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • MySQL安装常见报错处理方法总结大全

    MySQL安装常见报错处理方法总结大全

    MySQL数据库在安装或卸载的过程中,常常会出现一些错误,这是件让我们头疼的事,下面这篇文章主要给大家介绍了关于MySQL安装常见报错处理方法的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • mysql下怎样运行脚本以运行niuzi.sql为例

    mysql下怎样运行脚本以运行niuzi.sql为例

    mysql下运行脚本,有两种方法,都是在命令行下进行的,需要的朋友可以记录下
    2014-07-07
  • MySQL 字符串拆分实例(无分隔符的字符串截取)

    MySQL 字符串拆分实例(无分隔符的字符串截取)

    这篇文章主要介绍了MySQL 字符串拆分实例(无分隔符的字符串截取),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02

最新评论