解决mysql创建数据库使出现的编码问题
观察mysql服务器的配置文件可以发现:在/etc/mysql/my.cnf中引用了conf.d中的文件,所以直接在/etc/mysql/conf.d/mysql.cnf进行修改。
首先查看一下mysql的编码,进入mysql命令行
show variables like 'char%';
+--------------------------+----------------------------+
| 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 | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
或者使用status查看mysql服务器状态:
status
mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper
Connection id: 3
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.22-0ubuntu18.04.1 (Ubuntu)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 3 hours 35 min 40 sec
这里需要在[mysqld]设置character-set-server=utf8
- 编辑
mysql.conf
文件vim mysql.conf
- 修改
mysql.conf
文件,设置MySQL服务器的默认编码为utf8
character-set-server = utf8;
然后重启mysql服务,在命令行输入
service mysql restart
如果你不想这么麻烦,可以在创建数据库的时候设置编码为utf8即可,可使用如下命令:
CREATE DATABSE db_name DEFUALT CHARACTER SET utf8;