1. Container code directory structure
test└ ─ docker - compose │ ├ ─ app │ │ Dockerfile │ │ │ └ ─ mysql │ └ ─ initdb │ erp_db. SQL └ ─ docker - compose. YmlCopy the code
1.1 APP /Dockerfile as an application is omitted in this article
1.2 erp_db. SQL
create database if not exists erp_db;
use erp_db;
CREATE TABLE `m_code` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
`kind` varchar(3) NOT NULL COMMENT 'classification',
`code` varchar(3) NOT NULL COMMENT 'coding',
`name` varchar(30) DEFAULT NULL COMMENT 'meaning'.PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='Encode master data'
;
INSERT INTO `m_code` (`id`, `kind`, `code`, `name`) VALUES (1.'PUR'.'001'.'Standard purchase');
INSERT INTO `m_code` (`id`, `kind`, `code`, `name`) VALUES (2.'PUR'.'002'.'Bulk purchase');
INSERT INTO `m_code` (`id`, `kind`, `code`, `name`) VALUES (3.'SAL'.'001'.'wholesale');
INSERT INTO `m_code` (`id`, `kind`, `code`, `name`) VALUES (4.'SAL'.'002'.'retail');
INSERT INTO `m_code` (`id`, `kind`, `code`, `name`) VALUES (5.'SAL'.'003'.'online');
Copy the code
1.3 docker – compose. Yml
services:
erp_server:
container_name: Mysql5.7 _erp
image: Mysql: 5.7
environment:
MYSQL_DATABASE: erp_db
MYSQL_USER: admin
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: rootpassword
expose:
- 3306
ports:
- 3306: 3306
# To prevent Chinese garbled characters, add a startup parameter
command: ['mysqld'.'--character-set-server=utf8mb4'.'--collation-server=utf8mb4_unicode_ci'.'--skip-character-set-client-handshake']
volumes:
# Start specifies how long it can be
- ./docker-compose/mysql/initdb:/docker-entrypoint-initdb.d/
5. # DB
- mysql_db:/var/lib/mysql
volumes:
mysql_db:
driver: local
Copy the code
2. Tread pit records
2.1 the phenomenon
After following the above arrangement, it is found that the data into the database is garbled
2.2 check the charset
show variables like '%chara%';
Copy the code
2.3 According to Charset, there are two ideas as follows
2.3.1 Idea 1: Change the red line part to UTF8, docker-comemage. yml append command
services:
erp_server:
container_name: Mysql5.7 _erp
image: Mysql: 5.7
command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
environment:
MYSQL_DATABASE: erp_db
MYSQL_USER: admin
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: rootpassword
expose:
- 3306
ports:
- 3306: 3306
volumes:
# Start specifies how long it can be
- ./docker-compose/mysql/initdb:/docker-entrypoint-initdb.d/
5. # DB
- mysql_db:/var/lib/mysql
volumes:
mysql_db:
driver: local
Copy the code
Let’s do it again
$ docker-compose down
$ docker-compose up -d
Copy the code
Note: if mysql is not reflected at this time
$ docker volume ls
$ docker volume rm [volume-name]
Copy the code
Make sure the data in the following table is better
2.3.2 Erp_db. SQL
Docker-comemess. yml is restored as erp_db.sql
create database if not exists erp_db;
use erp_db;
SET CHARACTER_SET_CLIENT = utf8;
SET CHARACTER_SET_CONNECTION = utf8;
CREATE TABLE `m_code` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
`kind` varchar(3) NOT NULL COMMENT 'classification',
`code` varchar(3) NOT NULL COMMENT 'coding',
`name` varchar(30) DEFAULT NULL COMMENT 'meaning'.PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='Encode master data'
;
INSERT INTO `m_code` (`id`, `kind`, `code`, `name`) VALUES (1.'PUR'.'001'.'Standard purchase');
INSERT INTO `m_code` (`id`, `kind`, `code`, `name`) VALUES (2.'PUR'.'002'.'Bulk purchase');
INSERT INTO `m_code` (`id`, `kind`, `code`, `name`) VALUES (3.'SAL'.'001'.'wholesale');
INSERT INTO `m_code` (`id`, `kind`, `code`, `name`) VALUES (4.'SAL'.'002'.'retail');
INSERT INTO `m_code` (`id`, `kind`, `code`, `name`) VALUES (5.'SAL'.'003'.'online');
Copy the code
Let’s do it again
$ docker-compose down
$ docker-compose up -d
Copy the code
Note: if mysql is not reflected at this time
$ docker volume ls
$ docker volume rm [volume-name]
Copy the code
Same as the original phenomenon
Reconfirm table data
Conclusion 3.
Modifying the configuration file or the container choreographer file did not solve the problem; Only in THE SQL context can the change finally solve the problem.