MySQL架構優化之字符集
MySQL架構優化之字符集
MySQL應用於大中小企業當中,字符集標準化也是MySQL架構優化中重要的一部分。通常建議中英文混合環境建議選擇utf8字元集。
1.作業系統Linux cat /etc/sysconfig/i18n => LANG="en_US.UTF-8"
2.MySQL客戶端 cat /etc/my.cnf => [client] default-character-set=utf8
3.MySQL服務端 cat /etc/my.cnf => [mysqld] character-set-server=utf8 collation-server=utf8_bin
4.庫、表字符集一致 預設庫、表字符集與MySQL服務端保持一致;所以,預設庫、表字符集均為utf8
5.程式 選擇統一的utf8程式安裝包
1.作業系統Linux字符集
[root@db12cvm1 ~]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
2.MySQL客戶端字符集
[root@db12cvm1 ~]# cat /etc/my.cnf
[client]
default-character-set=utf8
3.MySQL服務端字符集
[root@db12cvm1 ~]# cat /etc/my.cnf
[mysqld]
character-set-server=utf8
collation-server=utf8_bin
4.庫、表字符集一致 預設庫、表字符集與MySQL服務端保持一致
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database dbadoudou;
Query OK, 1 row affected (0.00 sec)
mysql> show create database dbadoudou\G;
*************************** 1. row ***************************
Database: dbadoudou
Create Database: CREATE DATABASE `dbadoudou` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */
1 row in set (0.00 sec)
ERROR:
No query specified
## DATABASE DEFAULT CHARACTER SET utf8 COLLATE utf8_bin and [mysqld] character-set-server=utf8
## collation-server=utf8_bin the same
mysql> use dbadoudou;
Database changed
mysql> create table dbadoudou
-> (
-> id int(4) not null auto_increment,
-> name char(20) not null,
-> primary key (id)
-> );
Query OK, 0 rows affected (4.36 sec)
mysql> show create table dbadoudou;
+-----------
+------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------+
| Table | Create Table
|
+-----------
+------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------+
| dbadoudou | CREATE TABLE `dbadoudou` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` char(20) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
+-----------
+------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
## TABLE DEFAULT CHARSET=utf8 COLLATE=utf8_bin AND [mysqld] character-set-server=utf8
## collation-server=utf8_bin the same
mysql> insert into dbadoudou (name) values('dbadoudou'),('ocmxiaoyu'),('ocmdream');
ERROR 1665 (HY000): Cannot execute statement: impossible to write to binary log since BINLOG_FORMAT =
STATEMENT and at least one table uses a storage engine limited to row-based logging. InnoDB is limited
to row-logging when transaction isolation level is READ COMMITTED or READ UNCOMMITTED.
## CAUSE: ERROR 1665 (HY000) SOLUTION: SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
mysql> show global variables like 'binlog_format';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)
mysql> SELECT @@session.tx_isolation, @@global.tx_isolation;
+------------------------+-----------------------+
| @@session.tx_isolation | @@global.tx_isolation |
+------------------------+-----------------------+
| READ-COMMITTED | READ-COMMITTED |
+------------------------+-----------------------+
1 row in set (0.00 sec)
mysql> SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@session.tx_isolation, @@global.tx_isolation;
+------------------------+-----------------------+
| @@session.tx_isolation | @@global.tx_isolation |
+------------------------+-----------------------+
| REPEATABLE-READ | READ-COMMITTED |
+------------------------+-----------------------+
1 row in set (0.00 sec)
mysql> insert into dbadoudou (name) values('dbadoudou'),('ocmxiaoyu'),('ocmdream');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from dbadoudou;
+----+-----------+
| id | name |
+----+-----------+
| 1 | dbadoudou |
| 2 | ocmxiaoyu |
| 3 | ocmdream |
+----+-----------+
3 rows in set (0.00 sec)
## reference:MOS (文件 ID 1433907.1)
5.處理MySQL亂碼
上面講到字符集要標準化保持一致。如果出現不一致,怎麼解決亂碼問題呢?
臨時解決:
set names utf8;
永久解決:
vi /etc/my.cnf
[mysqld]
character-set-server=utf8
collation-server=utf8_bin
MySQL應用於大中小企業當中,字符集標準化也是MySQL架構優化中重要的一部分。通常建議中英文混合環境建議選擇utf8字元集。
1.作業系統Linux cat /etc/sysconfig/i18n => LANG="en_US.UTF-8"
2.MySQL客戶端 cat /etc/my.cnf => [client] default-character-set=utf8
3.MySQL服務端 cat /etc/my.cnf => [mysqld] character-set-server=utf8 collation-server=utf8_bin
4.庫、表字符集一致 預設庫、表字符集與MySQL服務端保持一致;所以,預設庫、表字符集均為utf8
5.程式 選擇統一的utf8程式安裝包
1.作業系統Linux字符集
[root@db12cvm1 ~]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
2.MySQL客戶端字符集
[root@db12cvm1 ~]# cat /etc/my.cnf
[client]
default-character-set=utf8
3.MySQL服務端字符集
[root@db12cvm1 ~]# cat /etc/my.cnf
[mysqld]
character-set-server=utf8
collation-server=utf8_bin
4.庫、表字符集一致 預設庫、表字符集與MySQL服務端保持一致
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database dbadoudou;
Query OK, 1 row affected (0.00 sec)
mysql> show create database dbadoudou\G;
*************************** 1. row ***************************
Database: dbadoudou
Create Database: CREATE DATABASE `dbadoudou` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */
1 row in set (0.00 sec)
ERROR:
No query specified
## DATABASE DEFAULT CHARACTER SET utf8 COLLATE utf8_bin and [mysqld] character-set-server=utf8
## collation-server=utf8_bin the same
mysql> use dbadoudou;
Database changed
mysql> create table dbadoudou
-> (
-> id int(4) not null auto_increment,
-> name char(20) not null,
-> primary key (id)
-> );
Query OK, 0 rows affected (4.36 sec)
mysql> show create table dbadoudou;
+-----------
+------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------+
| Table | Create Table
|
+-----------
+------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------+
| dbadoudou | CREATE TABLE `dbadoudou` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` char(20) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
+-----------
+------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
## TABLE DEFAULT CHARSET=utf8 COLLATE=utf8_bin AND [mysqld] character-set-server=utf8
## collation-server=utf8_bin the same
mysql> insert into dbadoudou (name) values('dbadoudou'),('ocmxiaoyu'),('ocmdream');
ERROR 1665 (HY000): Cannot execute statement: impossible to write to binary log since BINLOG_FORMAT =
STATEMENT and at least one table uses a storage engine limited to row-based logging. InnoDB is limited
to row-logging when transaction isolation level is READ COMMITTED or READ UNCOMMITTED.
## CAUSE: ERROR 1665 (HY000) SOLUTION: SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
mysql> show global variables like 'binlog_format';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)
mysql> SELECT @@session.tx_isolation, @@global.tx_isolation;
+------------------------+-----------------------+
| @@session.tx_isolation | @@global.tx_isolation |
+------------------------+-----------------------+
| READ-COMMITTED | READ-COMMITTED |
+------------------------+-----------------------+
1 row in set (0.00 sec)
mysql> SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@session.tx_isolation, @@global.tx_isolation;
+------------------------+-----------------------+
| @@session.tx_isolation | @@global.tx_isolation |
+------------------------+-----------------------+
| REPEATABLE-READ | READ-COMMITTED |
+------------------------+-----------------------+
1 row in set (0.00 sec)
mysql> insert into dbadoudou (name) values('dbadoudou'),('ocmxiaoyu'),('ocmdream');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from dbadoudou;
+----+-----------+
| id | name |
+----+-----------+
| 1 | dbadoudou |
| 2 | ocmxiaoyu |
| 3 | ocmdream |
+----+-----------+
3 rows in set (0.00 sec)
## reference:MOS (文件 ID 1433907.1)
5.處理MySQL亂碼
上面講到字符集要標準化保持一致。如果出現不一致,怎麼解決亂碼問題呢?
臨時解決:
set names utf8;
永久解決:
vi /etc/my.cnf
[mysqld]
character-set-server=utf8
collation-server=utf8_bin
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26442936/viewspace-2078573/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL架構的優化MySql架構優化
- RedisCluster架構優化Redis架構優化
- MySQL 高可用架構之 MMM 架構MySql架構
- MySQL資料庫效能優化之表結構優化(轉)MySql資料庫優化
- MySQL 效能優化之索引優化MySql優化索引
- MySQL 效能優化之SQL優化MySql優化
- 精通MySQL之架構篇MySql架構
- MySQL之基礎架構MySql架構
- MySQL 之開放架構MySql架構
- MySQL調優之索引優化MySql索引優化
- Vue專案架構優化Vue架構優化
- 前端架構師必備之Vue專案打包優化前端架構Vue優化
- MySQL優化之系統變數優化MySql優化變數
- MySQL調優之查詢優化MySql優化
- MySQL之SQL優化技巧MySql優化
- mysql優化之explain 指令MySql優化AI
- MySQL優化之索引解析MySql優化索引
- WebRTC 架構優化及實踐Web架構優化
- 系統架構效能優化思路架構優化
- 秒殺業務架構優化之路架構優化
- 菜鳥要做架構師(二)——java效能優化之for迴圈架構Java優化
- MySQL 效能優化之快取引數優化MySql優化快取
- mysql學習之-邏輯架構MySql架構
- MySQL高可用架構之Keepalived+主從架構部署MySql架構
- MySQL之SQL語句優化MySql優化
- 【MySQL】效能優化之 order by (一)MySql優化
- 搜狐服務架構優化實踐架構優化
- 架構 秒殺系統優化思路架構優化
- 秒殺系統架構優化思路架構優化
- Mysql之讀寫分離架構-AtlasMySql架構
- MySQL 實現高可用架構之 MHAMySql架構
- MySQL高可用架構之MHA實踐MySql架構
- MySQL高可用架構之PXC實踐MySql架構
- MySql架構MySql架構
- 架構之:serverless架構架構Server
- 《MySQL效能優化和高可用架構實踐》簡介與推薦序MySql優化架構
- 《MySQL 效能優化》之理解 MySQL 體系結構MySql優化
- MySQL調優篇 | 邏輯架構解讀(1)MySql架構