MySQL字符集

作者: 诺之林 | 来源:发表于2018-04-16 17:17 被阅读25次

MySQL

brew install mysql

brew services start mysql

brew services list

DB1

mysql -uroot -p
CREATE DATABASE IF NOT EXISTS db1
DEFAULT CHARSET utf8 COLLATE utf8_unicode_ci;

USE db1;
CREATE TABLE t_demo (
id int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
content varchar(255) NOT NULL
) DEFAULT CHARSET=utf8;
INSERT INTO t_demo ( content ) VALUES ( 'hello' );
# Query OK, 1 row affected (0.00 sec)

INSERT INTO t_demo ( content ) VALUES ( '😀' );
# ERROR 1366 (HY000): Incorrect string value: '\xF0\x9F\x98\x80' for column 'content' at row 1
TRUNCATE TABLE t_demo;

ALTER TABLE t_demo CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
INSERT INTO t_demo ( content ) VALUES ( 'hello' );
# Query OK, 1 row affected (0.00 sec)

INSERT INTO t_demo ( content ) VALUES ( '😀' );
# ERROR 1366 (HY000): Incorrect string value: '\xF0\x9F\x98\x80' for column 'content' at row 1

my.cnf

vim /usr/local/etc/my.cnf
[client]
default-character-set=utf8mb4

[mysqld]
bind-address = 127.0.0.1

[mysql]
default-character-set = utf8mb4
brew services restart mysql
mysql -uroot -p
mysql> show variables like 'character_set_%';
+--------------------------+------------------------------------------------------+
| Variable_name            | Value                                                |
+--------------------------+------------------------------------------------------+
| character_set_client     | utf8mb4                                              |
| character_set_connection | utf8mb4                                              |
| character_set_database   | utf8                                                 |
| character_set_filesystem | binary                                               |
| character_set_results    | utf8mb4                                              |
| character_set_server     | utf8                                                 |
| character_set_system     | utf8                                                 |
| character_sets_dir       | /usr/local/Cellar/mysql/5.7.21/share/mysql/charsets/ |
+--------------------------+------------------------------------------------------+
8 rows in set (0.00 sec)

DB2

CREATE DATABASE IF NOT EXISTS db2
DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE db2;
CREATE TABLE t_demo (
id int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
content varchar(255) NOT NULL
) DEFAULT CHARSET=utf8mb4;
INSERT INTO t_demo ( content ) VALUES ( 'hello' );
# Query OK, 1 row affected (0.00 sec)

INSERT INTO t_demo ( content ) VALUES ( '😀' );
# Query OK, 1 row affected (0.01 sec)
TRUNCATE TABLE t_demo;

ALTER TABLE t_demo CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
INSERT INTO t_demo ( content ) VALUES ( 'hello' );
# Query OK, 1 row affected (0.00 sec)

INSERT INTO t_demo ( content ) VALUES ( '😀' );
# ERROR 1366 (HY000): Incorrect string value: '\xF0\x9F\x98\x80' for column 'content' at row 1

如果使用远程MySQL服务, 可以将端口映射到本地: ssh -L 3306:127.0.0.1:3306 -l saas 192.168.56.222

参考

相关文章

  • 字符集与校对规则

    MySQL字符集及校对规则的理解 阅读目录:MySQL的字符集和校对规则 MySQL的字符集 MySQL与字符集 ...

  • MySQL字符排序

    关于MySQL字符集 可以参考文章MySQL字符集 & 重谈MySQL字符集 目录 大小写 比较规则 大小写 _c...

  • Mysql全备、增量备份及恢复

    Mysql字符集 MySQL数据库字符集包括字符集(CHARACTER)和校对规则(COLLATION)两个概念,...

  • MySQL常见问题汇集

    字符集mysql 字符集(CHARACTER SET)和校对集(COLLATE)mysql 中 character...

  • Mysql字符集设置

    mysql字符集编码

  • 八、MySQL字符集

    字符集和排序规则简介   MySQL提供了多种字符集和排序规则选择,其中字符集设置和数据存储以及客户端与MySQL...

  • MySQL乱码问题

    MySQL字符集参数 MySQL参数使用 影响到字符编码的设置 数据库级别的字符集信息使用db.opt来存放字符集...

  • 字符集

    定义 mysql 的字符集包括 字符集 CHARACTER 和 校对规则COLLATION 两个概念 字符集 : ...

  • mysql 字符集设置查看

    1.列出MYSQL支持的所有字符集:SHOW CHARACTER SET; 2.当前MYSQL服务器字符集设置SH...

  • MYSQL字符集设置

    MYSQL的Workbench设置字符集失败,可以用以下命令行方式设置MYSQL字符集: 在my.cnf中设置: ...

网友评论

    本文标题:MySQL字符集

    本文链接:https://www.haomeiwen.com/subject/oulekftx.html