Home > Database > Mysql Tutorial > 关于mysql乱码的一些解决方法_MySQL

关于mysql乱码的一些解决方法_MySQL

WBOY
Release: 2016-06-01 13:43:17
Original
1141 people have browsed it

bitsCN.com

新建了一个库,默认编码。再导入utf8编码的表和数据。结果全是乱码。改sql文件的编码,还是不行。最后发现竟然是建库的时候没有设置utf8的编码。于是乎有了如下的的记录,问题得以解决。

建库:

    CREATE DATABASE `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

另记录相关数据库操作:
导入sql文件

    mysql -h localhost -u root -p mydbname

导出sql

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

建表:

    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;

插入数据:

    LOCK TABLES `mytablename` WRITE;
    INSERT INTO `mytablename` (`id`,`int10`,`char2`,`varchar100`) VALUES (1,123456,"ab","adsfsadf");
    UNLOCK TABLES;

创建用户(设权限,密码)

    GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' IDENTIFIED BY '123456′;
    GRANT ALL PRIVILEGES ON *.* TO 'user'@'127.0.0.1′ IDENTIFIED BY '123456′;

ALL PRIVILEGES 为所有权限,可选:select,insert,update,delete,create,drop等
*.* 表示:某个库.某个表
localhost为只能在本地登录,若为任意地点设为: %

修改密码:

    mysqladmin -u root -p password 456789

查看所有用户:

    select host,user from mysql.user;

 

最后,为了避免乱码的出现应该做到:文件编码+页面编码+数据库编码+应用服务器编码+操作系统编码统一。


作者 空云万里晴

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template