新建了一個庫,默認編碼。再導入utf8編碼的表和數據。結果全是亂碼。改sql文件的編碼,還是不行。最後發現竟然是建庫的時候沒有設置utf8的編碼。於是乎有了如下的的記錄,問題得以解決。
建庫:
CREATE DATABASE `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
另記錄相關數據庫操作:
導入sql文件
mysql -h localhost -u root -p mydbname < data.sql;
導出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;
最後,為了避免亂碼的出現應該做到:文件編碼+頁面編碼+數據庫編碼+應用服務器編碼+操作系統編碼統一。
作者 空雲萬裡晴