欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-29 09:06:04
mysql字符集编码错误的导入数据会提示错误了,这个和插入数据一样如果保存的数据与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字符集导致恢复数据库报错问题的全部内容,希望对大家有所帮助。有什么问题可以随时留言,小编会及时回复大家。感谢朋友们对本站的支持!