MySQL(MariaDB)修改默認編碼

登陸進入數據庫,本機測試數據庫沒有設置密碼。其餘密碼登陸請執行命令。(假設服務器是127.0.0.1,用戶名是root)mysql

這裏輸入代碼

[root@localhost ~]mysql -h 127.0.0.1 -u root -psql

[root@localhost ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10176
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show variables like 'collation_%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
#修改數據庫字符集
MariaDB [(none)]>use cacti
MariaDB [(none)]> alter database cacti character set utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

MariaDB [(none)]>

修改數據庫和服務器編碼。數據庫

MariaDB [(none)]> set character_set_database=utf8;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> set character_set_server=utf8;
Query OK, 0 rows affected (0.00 sec)
#修改查詢結果編碼
MariaDB [(none)]> SET character_set_results = utf8;
Query OK, 0 rows affected (0.00 sec)

建立數據庫指定數據庫並字符集。服務器

mysql>create database mydb character set utf-8;

修改編碼也能夠修改MySQL數據庫配置文件並重啓服務,本文不加以介紹。測試

相關文章
相關標籤/搜索