MySQL資料庫中文亂碼問題

來源:互聯網
上載者:User

標籤:ror   for   清華大學   建庫   eid   rest   let   with   style   

mysql> select * from books;+-----+---------------------------------+---------+-------------+-------+------------+--------+-------------+| bId | bName                           | bTypeId | publishing  | price | pubDate    | author | ISBN        |+-----+---------------------------------+---------+-------------+-------+------------+--------+-------------+|   1 | ???????                         | 2       | ????????    |    34 | 2004-10-01 | ??     | 7505380796  ||   2 | ???????                         | 6       | ???????     |    41 | 2002-07-01 | ???    | 7121010925  ||   3 | ????????asp                     | 2       | ?????????   |    43 | 2005-02-01 | ??     | 75053815x   ||   4 | pagemaker 7.0??????             | 9       | ???????     |    43 | 2005-01-01 | ???    | 7121008947  ||   5 | ????????                        | 6       | ?????????   |    44 | 2003-06-29 | ???    | 7120000233  ||   6 | Dreamweaver 4?????              | 2       | ???????     |    44 | 2004-06-01 | ???    | 7505397699  |

1.確認並設定MySql的資料庫的編碼(注意下面橘色字型)mysql> status--------------mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1Connection id:2Current database:schoolCurrent user:[email protected]SSL:Not in useCurrent pager:stdoutUsing outfile:‘‘Using delimiter:;Server version:5.1.73 Source distributionProtocol version:10Connection:Localhost via UNIX socketServer characterset:utf8Db characterset:utf8Client characterset:latin1Conn. characterset:latin1UNIX socket:/var/lib/mysql/mysql.sockUptime:1 min 8 secThreads: 1 Questions: 13 Slow queries: 0 Opens: 17 Flush tables: 1 Open tables: 10 Queries per second avg: 0.191--------------
2.修改方法首先進入資料庫mysql -uroot -p123 mysql>use schoolmysql> alter database school character set utf8;mysql> alter database school character set utf8 collate utf8_general_ci;
mysql> alter table books character set utf8 collate utf8_general_ci;3.vim /etc/my.conf //編輯修改mysql設定檔[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0character-set-server = utf8[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid[client]default-character-set=utf8[mysql]default-character-set=utf84.以上儲存後重啟資料庫[[email protected] ~]# /etc/init.d/mysqld restart停止 mysqld: [確定]正在啟動 mysqld: [確定]5.進入mysql查看字元集mysql> show variables like "%char%";+--------------------------+----------------------------+| 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.00 sec)mysql> show variables like "%colla%";+----------------------+-----------------+| Variable_name | Value |+----------------------+-----------------+| collation_connection | utf8_general_ci || collation_database | utf8_general_ci || collation_server | utf8_general_ci |+----------------------+-----------------+3 rows in set (0.00 sec)
mysql> select * from books;+-----+---------------------------------------------------------+---------+-----------------------------------+-------+------------+-----------+-------------+| bId | bName                                                   | bTypeId | publishing                        | price | pubDate    | author    | ISBN        |+-----+---------------------------------------------------------+---------+-----------------------------------+-------+------------+-----------+-------------+|   1 | 網站製作直通車                                          | 2       | 電腦愛好者雜誌社                  |    34 | 2004-10-01 | 苗壯      | 7505380796  ||   2 | 駭客與網路安全                                          | 6       | 航空工業出版社                    |    41 | 2002-07-01 | 白立超    | 7121010925  ||   3 | 網路程式與設計-asp                                     | 2       | 北方交通大學出版社                |    43 | 2005-02-01 | 王玥      | 75053815x   ||   4 | pagemaker 7.0短期培訓教程                               | 9       | 中國電力出版社                    |    43 | 2005-01-01 | 孫利英    | 7121008947  ||   5 | 駭客攻擊防範秘笈                                        | 6       | 北京騰圖電子出版社                |    44 | 2003-06-29 | 趙雷雨    | 7120000233  ||   6 | Dreamweaver 4入門與提高                                 | 2       | 清華大學出版社                    |    44 | 2004-06-01 | 嶽玉博    | 7505397699  |

一、避免建立資料庫及表出現中文亂碼和查看編碼方法
1、建立資料庫的時候:CREATE DATABASE `test`
CHARACTER SET ‘utf8‘
COLLATE ‘utf8_general_ci‘;
2、建表的時候 CREATE TABLE `database_user` (
`ID` varchar(40) NOT NULL default ‘‘,
`UserID` varchar(40) NOT NULL default ‘‘,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

即建庫和建表時都使用相同的編碼格式。  

查看school資料庫的編碼格式:

mysql> show create database school;+----------+-----------------------------------------------------------------+| Database | Create Database                                                 |+----------+-----------------------------------------------------------------+| school   | CREATE DATABASE `school` /*!40100 DEFAULT CHARACTER SET utf8 */ |+----------+-----------------------------------------------------------------+1 row in set (0.00 sec)

 查看books表的編碼格式

 1 mysql> use school 2 Reading table information for completion of table and column names 3 You can turn off this feature to get a quicker startup with -A 4  5 Database changed 6 mysql> show create table books; 7 +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 8 | Table | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                               | 9 +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+10 | books | CREATE TABLE `books` (11   `bId` int(4) NOT NULL AUTO_INCREMENT,12   `bName` varchar(255) DEFAULT NULL,13   `bTypeId` enum(‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘10‘) DEFAULT NULL,14   `publishing` varchar(255) DEFAULT NULL,15   `price` int(4) DEFAULT NULL,16   `pubDate` date DEFAULT NULL,17   `author` varchar(30) DEFAULT NULL,18   `ISBN` varchar(255) DEFAULT NULL,19   PRIMARY KEY (`bId`)20 ) ENGINE=MyISAM AUTO_INCREMENT=45 DEFAULT CHARSET=utf8 |21 +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+22 1 row in set (0.00 sec)

 

MySQL資料庫中文亂碼問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.