mysql5.7 windows7編碼統一utf-8
阿新 • • 發佈:2019-01-07
檢視mysql資料編碼
登入mysql服務,檢視mysql資料庫預設的編碼
mysql> show variables like 'character%';
+--------------------------+-------------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_ results | gbk |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | E:\0Develop\mysql-5.7.15-winx64\share\charsets\ |
+--------------------------+-------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)
mysql>
可以看出database和server都採用了latin1編碼,不支援中文,我們需要把它修改為utf-8。
除了這兩個,其它的如下
| 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 | E:\0Develop\mysql-5.7.15-winx64\share\charsets\
設定mysql資料庫編碼
在mysql安裝目錄下,將 my-default.ini 複製一分儲存為 my.ini
做如下修改
主要看紅色框裡的內容
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
[mysql]
default-character-set = utf8
[mysql.server]
default-character-set = utf8
[mysqld_safe]
default-character-set = utf8
[client]
default-character-set = utf8
然後刪除你當前的mysql服務,再重新註冊mysql服務
注:一定要以管理員的許可權執行dos命令,且cd到mysql安裝目錄下的bin目錄,再註冊mysql服務
C:\Windows\system32>sc delete mysql
[SC] DeleteService 成功
C:\Windows\system32>e: cd
E:\>cd E:\0Develop\mysql-5.7.15-winx64\bin
E:\0Develop\mysql-5.7.15-winx64\bin>mysqld --install mysql --defaults-file="E:\0Develop\mysql-5.7.15-winx64\my.ini"
The service already exists!
The current server installed: E:\0Develop\mysql-5.7.15-winx64\bin\mysqld MySQL
E:\0Develop\mysql-5.7.15-winx64\bin>net stop mysql
MySQL 服務正在停止..服務名無效。
請鍵入 NET HELPMSG 2185 以獲得更多的幫助。
E:\0Develop\mysql-5.7.15-winx64\bin>mysqld --install mysql --defaults-file="E:\0Develop\mysql-5.7.15-winx64\my.ini"
Service successfully installed.
E:\0Develop\mysql-5.7.15-winx64\bin>
結果
啟動服務,檢視編碼
C:\Windows\System32>net start mysql
mysql 服務正在啟動 .
mysql 服務已經啟動成功。
C:\Windows\System32>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.15 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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%';
+--------------------------+-------------------------------------------------+
| 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 | E:\0Develop\mysql-5.7.15-winx64\share\charsets\ |
+--------------------------+-------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)
mysql>
資料庫例項編碼設定
mysql> show create database test;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter database test default character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql> show create database test;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| test | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>