Solutions to mysql garbled characters

Source: Internet
Author: User

A new library is created with the default encoding. Import utf8 encoded tables and data. The results are all garbled. Changing the encoding of the SQL file still does not work. Finally, we found that utf8 encoding was not set during database creation. As a result, the following records are recorded to solve the problem.

Database creation:

Create database 'mydb' default character set utf8 COLLATE utf8_general_ci;

Record related database operations:
Import SQL files

Mysql-h localhost-u root-p mydbname <data. SQL;

Export SQL

Mysqldump-h localhost-u root-p -- default-character-set = utf8 mydbname> data. SQL;

Table creation:

Drop table if exists 'mytablename ';
SET @ saved_cs_client =@@ character_set_client;
SET character_set_client = utf8;
Create table 'mytablename '(
'Id' int (10) unsigned not null auto_increment,
'Int10' int (10) unsigned not null,
'Char2' char (2 ),
'Varchar100' varchar (100) not null,
Primary key ('id ')
) ENGINE = MyISAM AUTO_INCREMENT = 1 default charset = utf8;
SET character_set_client = @ saved_cs_client;

Insert data:

Lock tables 'mytablename' WRITE;
Insert into 'mytablename' ('id', 'int10', 'char2', 'varchar100') VALUES (, "AB", "adsfsadf ");
Unlock tables;

Create a user (Set permissions and passwords)

Grant all privileges on *. * TO 'user' @ 'localhost' identified by '123 ′;
Grant all privileges on *. * TO 'user' @ '1970. 0.0.1 'identified by '123 ′;

All privileges indicates ALL permissions. Optional values: select, insert, update, delete, create, and drop.
*. * Indicates a database. A table.
Localhost is set to: %

Change Password:

Mysqladmin-u root-p password 456789

View All Users:

Select host, user from mysql. user;

 

Finally, in order to avoid garbled characters, file encoding, page encoding, database encoding, Application Server encoding, and operating system encoding should be unified.


Author: Kong Yun Wanqing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.