1.docker run -d -p 3306:3306 --privileged=true \
-v /abc/mysql/log:/var/log/mysql \
-v /abc/mysql/data:/var/lib/mysql \
-v /abc/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=12345678 \
--name=mysql \
mysql:5.7
2.新建my.cnf
cd /abc/mysql/conf
vi my.cnf
加入
[client]
default_character_set=utf8
[mysqld]
collation_server=utf8_general_ci
character_set_server=utf8
3.重新啟動mysql容器,再進入查看字符編碼
docker restart mysql
docker exec -it mysql bash
mysql -uroot -p
SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';
4.測試,看中文是否為亂碼
show databases;
create database db01;
use db01;
create table t1(id int,name varchar(40));
insert into t1 values(1,'u3');
insert into t1 values(2,'王五');
select * from t1;
--------------------------------------------------------------
安裝mysql 8
docker run --name 容器名稱 -d mysql --net=網路名稱 -p xxxx:xxxx --privileged=true \
-e MYSQL_ROOT_PASSWORD=MYSQL的管理員密碼 \
-e MYSQL_DATABASE=資料庫網域名稱 \
-e MYSQL_USER=MYSQL的使用者名稱 \
-e MYSQL_PASSWORD=MYSQL的使用者密碼 \
--character-set-server=字元編碼 \
--collationserver=字符對照表 \
--default-authentication-plugin=驗證方式
--------------------------------------------------------
例如
docker network create netmysql
docker run --name mymysql -d --net=netmysql -p 3306:3306 --privileged=true \
-e MYSQL_ROOT_PASSWORD='12345678' \
-e MYSQL_DATABASE=sctmysql \
-e MYSQL_USER=sct \
-e MYSQL_PASSWORD='sct123456' \
mysql \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci \
--default-authentication-plugin=mysql_native_password
=============================================
docker run -d -p 3306:3306 --privileged=true \
-v /abc/mysql/log:/var/log/mysql \
-v /abc/mysql/data:/var/lib/mysql \
-v /abc/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=12345678 \
-e MYSQL_DATABASE=sctmysql \
-e MYSQL_USER=sct \
-e MYSQL_PASSWORD='sct123456' \
--name=mysql \
mysql
vi my.cnf
加入
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
collation-server=utf8mb4_unicode_ci
character-set-server = utf8mb4
character-set-client-handshake = FALSE
沒有留言:
張貼留言