MySQL: Charset and Collation

1. Introduction

    1) create table table_name (column_declaration) charset utf8;

    2) set names gbk;

    Comments:

    1) What's the meaning?

    2) What's the difference?

 

2. Charset

    1) Charset hierarchy: (Server>Database>Table>Column)

        1) Server default charset

        2) Database default charset

        3) Table default charset

        4) Column default charset

    2) Charset hierarchy policy:

        1) If we didn't declare the charset for a specific level, then its charset inherit from its parent's.

        2) If we didn't declare the charset for server, the server can never start.

    3) Comprehension for Translator:

        1) Client/Console have its own charset        

        2) Translator has its own charset

        3) Database has its own charset 

 

    4) Translator:

        1) Translator has to know the input data's charset      ---> In the figure above, the charset from client/console is gbk.                        ---> character_set_client = gbk;

        2) Translator has to know the transit data's charset    ---> In the figure above, the charset for transit data is utf-8 marked as red.     ---> character_set_connection = utf8;

        3) Translator has to know the database/table charset ---> In the figure above, the charset for database is utf-8.                                ---> create table table_name (column_delcaration) charset utf8;

        4) Translator has to know the output data's charset    ---> In the figure above, the charset for output is gbk.                                       ---> character_set_results = gbk;

        Comments: If character_set_client, character_set_connection, character_set_results is the same value of N. Then we can use "set names N" for short.

    

    5) When will garbled occurs?

        1) Character_set_client is not according to the truth. The data input from console is in charset of gbk. If we declared character_set_client = utf8, garbled occured.

        2) Character_set_results is not according to the truth. The data output to webpage is in charset of utf8. If we declared character_set_client = gbk, garbled occured.

 

    6) When will data loss?

        1) Character_set_connection/database-charset is smaller than the charset of data passed from client.

        Eg: gbk->lartin1->gbk: During the procession of translating from client to transit data, data loss!

              gbk->gbk->lartin1: Durint the processon of translating from transit data to database, data loss!

    7) Real world problem:

         1) For some reason, the data store in database as charset gbk and cannot be modified.

         2) Data passed from client is php with charset of utf8.

         3) Solution: set names utf8;  crate table table_name(column_declaration) charset gbk;



 3. Collation

    1) Introduction

# Create table
create table temp(name varchar(12));
# Insert data
insert into temp values('a'), ('B'), ('c'), ('D');
# Order data
select * from temp order by name asc;
+------+
| name |
+------+
| a    |
| B    |
| c    |
| D    |
+------+
# Q: a->97, B->66. Why a < B?
# A: Refer to collation.

    2) What is collaton?

        1) In order to order data in a table according to a column, we must specify a rule for this. And the rule is just the collation.

    3) What is the relationship between charset and collation?

        1) One charset may have many collations.

# Command for show collation
show collation

# Command for show collation for utf8
show collation like 'utf8%'
# utf8 has about 40 collations.

         2) Default collation for utf8 is 'utf8_general_ci': Is case insensitive.

                                                       'utf8_bin': Order by binary code.(ASCII Code)

# Create table
create table temp2(name varchar(11)) charset utf8 collate=utf8_bin;
# Insert data
insert into temp2 values('a'), ('B'), ('c'), ('D');
# Order data
select * from temp2 order by name asc;
+------+
| name |
+------+
| B    |
| D    |
| a    |
| c    |
+------+

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些关于在CentOS 8上从源代码编译安装MySQL的步骤。 1. 安装编译MySQL所需的依赖项: ``` sudo dnf install gcc gcc-c++ ncurses-devel cmake make ``` 2. 下载MySQL源代码: 您可以从MySQL官方网站下载MySQL的源代码包,下载地址为:https://dev.mysql.com/downloads/mysql/ 3. 解压MySQL源代码包: ``` tar -zxvf mysql-VERSION.tar.gz cd mysql-VERSION ``` 4. 创建MySQL编译目录: ``` mkdir build cd build ``` 5. 运行cmake配置: ``` cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DSYSCONFDIR=/etc \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DWITH_SSL=system \ -DWITH_ZLIB=system \ -DWITH_LIBWRAP=0 \ -DENABLED_LOCAL_INFILE=1 \ -DENABLE_DTRACE=0 \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci ``` 6. 编译MySQL: ``` make ``` 7. 安装MySQL: ``` sudo make install ``` 8. 配置MySQL: ``` sudo groupadd mysql sudo useradd -r -g mysql -s /bin/false mysql sudo chown -R mysql:mysql /usr/local/mysql sudo chown -R mysql:mysql /usr/local/mysql/data sudo cp support-files/my-default.cnf /etc/my.cnf sudo vi /etc/my.cnf ``` 在my.cnf文件中,您可以根据需要进行配置。例如,您可以指定MySQL数据目录的位置,设置MySQL的端口等等。完成后,保存并退出。 9. 初始化MySQL: ``` sudo /usr/local/mysql/bin/mysqld --initialize --user=mysql ``` 10. 启动MySQL: ``` sudo /usr/local/mysql/bin/mysqld_safe --user=mysql & ``` 11. 登录MySQL: ``` sudo /usr/local/mysql/bin/mysql -uroot -p ``` 现在,您已经成功地在CentOS 8上从源代码编译安装了MySQL

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值