数椐库乱码问题

character_set_database和character_set_server的默认字符集是latin1

最简单的完美修改方法,修改mysql的my.cnf:

在[client]字段里加入default-character-set = utf8

在[mysqld]字段里加入character-set-server=utf8

vi /etc/my.cnf

[client]
#password       = your_password
default-character-set = utf8

[mysqld]
#skip-grant-tables
character-set-server = utf8

注意需要重启数据库:

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       | /alidata/server/mysql-5.5.40/share/charsets/ |
+--------------------------+----------------------------------------------+
8 rows in set (0.00 sec)

但是还是不能显示中:

mysql> insert into articles (id,content) values (15,‘全备‘);
Query OK, 1 row affected, 1 warning (0.22 sec)

mysql> select * from articles;
+----+---------+
| id | content |
+----+---------+
| 11 | ha      |
| 12 | xi      |
| 13 | ai      |
| 14 | ho      |
| 15 | ??      |
+----+---------+
5 rows in set (0.00 sec)
mysql> show create table articles;
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                   |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| articles | CREATE TABLE `articles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content` longtext NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1 |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

因为表的本身的字符集就是latin1

还有个问题  就是在mysql>的壮态下输入中文的时候会出现乱码 如:

经过测试,应变是系统的问题,关闭所有的shell,再重新开启问题解决!

=============================================================

之前做过的实验:
[[email protected] ~]# cat /etc/sysconfig/i18n
LANG="zh_CN.GB2312"
#LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
[[email protected] ~]# mysql -uroot -p123 -e "show variables like ‘character_set_%‘;"
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | gb2312                           |
| character_set_connection | gb2312                           |
| character_set_database   | utf8                             |
| character_set_filesystem | binary                           |
| character_set_results    | gb2312                           |
| character_set_server     | utf8                             |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
[[email protected] ~]# mysql -uroot -p123
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 5.5.20-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> show variables like ‘character_set_%‘;
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | gb2312                           |
| character_set_connection | gb2312                           |
| character_set_database   | utf8                             |
| character_set_filesystem | binary                           |
| character_set_results    | gb2312                           |
| character_set_server     | utf8                             |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

mysql> set names utf8;
Query OK, 0 rows affected (0.00 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/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

mysql>
mysql> exit #退出再登陆 发现又恢复原来的字符集所以,说set names ***是临时的
Bye
[[email protected] ~]# mysql -uroot -p123
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.5.20-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> show variables like ‘character_set_%‘;
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | gb2312                           |
| character_set_connection | gb2312                           |
| character_set_database   | utf8                             |
| character_set_filesystem | binary                           |
| character_set_results    | gb2312                           |
| character_set_server     | utf8                             |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

mysql> 下面如果代上--default-character-set=utf8 

[[email protected] ~]# mysql -uroot -p123 --default-character-set=utf8
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 33
Server version: 5.5.20-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

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/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

mysql> 其实它跟set names *** 的性质是一样的 只是在登录的时候就设置了(临时)
从则面也说明linux的字符集不是关键,建库和建表的字符集才是关键。
永久设置
[[email protected] ~]# sed -En ‘/client|default-characte/p‘ /etc/my.cnf
# The following options will be passed to all MySQL clients
[client]
default-character-set = utf8 #在client下增加
[[email protected] ~]# 看到linux系统的字符集如下
[[email protected] ~]# cat /etc/sysconfig/i18n
LANG="zh_CN.GB2312"
#LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
[[email protected] ~]# mysql -uroot -p123 -e "show variables like ‘character_set_%‘;"
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8           [clinet]          |
| character_set_connection | utf8            [clinet]         |
| character_set_database   | utf8          [mysqld]           |
| character_set_filesystem | binary                           |
| character_set_results    | utf8            [client]         |
| character_set_server     | utf8         [mysqld]            |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
可以看到字符集已经统一
最后是mysql的服务端:但是需要重启
[[email protected] ~]# sed -nE ‘/mysqld|^character-set/p‘ /etc/my.cnf
[mysqld]
character-set-server = utf8  #在/etc/my.cnf 的[mysqd]下增加
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# (via the "enable-named-pipe" option) will render mysqld useless!
[mysqldump]
只有重启才能把配置文件 加载到内存中
其中服务端是改的 character_set_database  和 character_set_server这两个。

也把linux系统的字符集也改过来:
[[email protected] ~]# cat /etc/sysconfig/i18n
#LANG="zh_CN.GB2312"
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
[[email protected] ~]# source /etc/sysconfig/i18n
时间: 2024-11-09 00:03:04

数椐库乱码问题的相关文章

查?看?和?设?置?o?r?a?c?l?e?数?据?库?的?最?大?连?接?数

当前的连接数 select count(*) from v$process; 设置的最大连接数(默认值为150) select value from v$parameter where name = 'processes'; 修改最大连接数 alter system set processes = 300 scope = spfile; 当数据库最大连接数不够时会出现客户端连接间歇性失败,报错ORA-12519. 查?看?和?设?置?o?r?a?c?l?e?数?据?库?的?最?大?连?接?数,布

s?q?l? ?s?e?r?v?e?r? ?2?0?0?0?登?录?名?与?数?据?库?用?户?名?的?关?联?问?题

MS SQL Server 2000 数据库使用备份还原造成的孤立用户和对象名'xxx'无效的错误的解决办法     在使用数据库的过程中,经常会遇到数据库迁移或者数据迁移的问题,或者有突然的数据库损坏,这时需要从数据库的备份中直接恢复.但是,此时会出现问题,这里说明几种常见问题的解决方法.  一.孤立用户的问题比如,以前的数据库的很多表是用户test建立的,但是当我们恢复数据库后,test用户此时就成了孤立用户,没有与之对应的登陆用户名,哪怕你建立了一个test登录用户名,而且是以前的用户密码

C?#? ?读?写?S?Q?L?数?据?库?I?m?a?g?e?字?段

数据库的Image字段保存的是字节,所以写入数据库Image字段和从数据库Image字段读取的内容都应该为字节. 1.数据库Image字段读写文件      写文件:写文件的过程为将文件以流文件形式打开并将内容读取到一个byte数组,然后将此byte数组写入数据库的Image字段. 源码:     FileInfo finfo=new FileInfo("文件名");   //绝对路径 if(finfo.Exists) {         SqlConnection conn=new 

volley开源库乱码问题总结(持续更新)

之前Android开发一直用的是多层封装的Final框架.最近开始学习使用小巧的volley. 在使用该框架的过程中,出现了数次乱码问题,再次做以总结. 分别是返回数据乱码和提交参数乱码两个问题: 一.返回数据乱码 该乱码问题比较常见. 使用StringRequest时,返回数据乱码,观察StringRequest类下源码: @Override protected Response<String> parseNetworkResponse(NetworkResponse response) {

NumPy-----函数------Matplotlib库

NumPy函数分为字符串函数,数学算数函数,算数运算和统计函数,以及排序.搜索和计数函数. 咱们依次讲解以上各类: 1.字符串行数 原文地址:https://www.cnblogs.com/qianshuixianyu/p/9245357.html

springmvc-数据库乱码-解决办法

第一步设置数据库连接 第二步设置数据库编码 /**c3p0取得数据源*/ public static DataSource getDataSource() throws Exception { // 创建连接池核心工具类 ComboPooledDataSource datasSource = new ComboPooledDataSource(); // 设置连接参数:url.驱动.用户密码.初始连接数.最大连接数 datasSource.setDriverClass("com.mysql.jd

MySQL数据库自带备份与恢复工具:MySQLdump.exe与mysql.exe

数据库的备份工作是保护数据库正常运行的关键,以下的文章主要讲述的是MySQL数据库备份的一些小妙招,我们大家都知道使用MySQL dump备份数据库的用户所需要的权限相对而言还是比较小的,只需要select与lock table的权限就足够了. 所以可以创建一个备份数据库的用户,专门用于备份数据库,这样可以避免数据库受到意外的破坏. MySQLdump是备份MySQL数据库的一种好工具.它相对于用phpmyadmin等备份工具更加快速,又避免受php.ini等的限制,在windows系统下还可以

MySQL数据库自带备份与恢复工具:MySQLdump.exe与mysql.exe

数据库的备份工作是保护数据库正常运行的关键,以下的文章主要讲述的是MySQL数据库备份的一些小妙招,我们大家都知道使用MySQL dump备份数据库的用户所需要的权限相对而言还是比较小的,只需要select与lock table的权限就足够了. 所以可以创建一个备份数据库的用户,专门用于备份数据库,这样可以避免数据库受到意外的破坏. MySQLdump是备份MySQL数据库的一种好工具.它相对于用phpmyadmin等备份工具更加快速,又避免受php.ini等的限制,在windows系统下还可以

Linux系统中“动态库”和“静态库”那点事儿 /etc/ld.so.conf 动态库的后缀为*.so 静态库的后缀为 libxxx.a ldconfig 目录名

Linux系统中“动态库”和“静态库”那点事儿 /etc/ld.so.conf  动态库的后缀为*.so  静态库的后缀为 libxxx.a   ldconfig   目录名 转载自:http://blog.chinaunix.net/uid-23069658-id-3142046.html 今天我们主要来说说Linux系统下基于动态库(.so)和静态(.a)的程序那些猫腻.在这之前,我们需要了解一下源代码到可执行程序之间到底发生了什么神奇而美妙的事情. 在Linux操作系统中,普遍使用ELF格