mysql 服务器搭建

mysql 服务器搭建

一、mysql容器安装

docker run -d -p 3306:3306 --restart always --privileged=true --name yunz_mysql -e MYSQL_ROOT_PASSWORD="密码" -v=/opt/data-mysql/config/my.cnf:/etc/my.cnf -v=/opt/data-mysql/data:/var/lib/mysql mysql:5.7

注意事项
手动创建挂载的配置目录,虽然docker会自动创建,但有时会不合适,尽量先手动创建好
把mysql的自定义配置提前准备,以my.cnf命名,放在/opt/data-mysql/config/my.cnf目录中
mysql容器建好后默认为UTC时区,需要修改mysql为北京时区

二、 my.cnf配置内容

主要配置了以下内容:

  1. 设置编码格式为utf-8
  2. 设置 表名大小写不敏感
  3. 设置 sql_mode,不进行严格的语法检查,比如select中返回字段必须满足group by中的字段等
  4. 设置时区为北京时区
  5. 设置数据文件容器内的路径,便于挂载
[client]
    default-character-set = utf8mb4
[mysql]
    default-character-set = utf8mb4

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql # 数据文件位置
socket=/var/lib/mysql/mysql.sock

#设置大小写不敏感
lower_case_table_names = 1

character-set-client-handshake = FALSE # true:跳过mysql程序起动时的字符参数设置 ,使用服务器端字符集设置
character-set-server = utf8mb4         
collation-server = utf8mb4_unicode_ci 
init_connect='SET NAMES utf8mb4'

# 设置SQL的语法支持
sql_mode = ""
# 设置默认时区
default-time_zone = '+8:00' 

三、踩坑之旅

为什么使用utf8mb64

mysql是有utf-8的但是它提供的utf-8字符集不全,所以后续又新增了 "utf8mb64"用于扩展原来的utf-8,utf8mb64符合现在实际的utf-8的编码。
mysql数据库连接指定编码方式使用utf8mb4不能用utf-8,因为utf-8对一部分字符(特别是表情)会识别错误,导致数据插入保存

你可能感兴趣的:(mysql)