使用Docker安装mysql,挂载外部配置和数据

1、挂载外部配置和数据安装

 #在home目录下创建mysql文件夹,下面创建data和conf.d文件夹
  mkdir /home/mysql
  mkdir /home/mysql/conf.d
  mkdir /home/mysql/data/
   
  创建my.cnf配置文件
  touch /home/mysql/my.cnf
  ​
  my.cnf添加如下内容:
  [mysqld]
  user=mysql
  character-set-server=utf8
  default_authentication_plugin=mysql_native_password
  secure_file_priv=/var/lib/mysql
  expire_logs_days=7
  sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
  max_connections=1000[client]
  default-character-set=utf8
  ​
  [mysql]
  default-character-set=utf8

2、创建容器,并后台启动

docker run --restart=always --privileged=true -d -v /home/mysql/data/:/var/lib/mysql -v /home/mysql/conf.d:/etc/mysql/conf.d -v /home/mysql/my.cnf:/etc/mysql/my.cnf -p 33306:3306 --name my-mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
  ​
  ​
  ​
  docker run  -di -v /home/mysql/data/:/var/lib/mysql -v /home/mysql/conf.d:/etc/mysql/conf.d -v /home/mysql/my.cnf:/etc/mysql/my.cnf -p 33306:3306 --name my-mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
  ​
  参数说明:
  --restart=always: 当Docker 重启时,容器会自动启动。
  --privileged=true:容器内的root拥有真正root权限,否则容器内root只是外部普通用户权限
  -v /home/mysql/conf.d/my.cnf:/etc/my.cnf:映射配置文件
  -v /home/mysql/data/:/var/lib/mysql:映射数据目录
  

3、可能错误

注意:启动mysql报如下错误,那是因为MYSQL新特性secure_file_priv对读写文件的影响。

ERROR: mysqld failed while attempting to check config
  command was: "mysqld --verbose --help"
  ​
  mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)
  2019-09-14T09:52:51.015937Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
  2019-09-14T09:52:51.018328Z 0 [ERROR] [MY-010119] [Server] Aborting
  
解决问题:
windows下:修改my.ini 在[mysqld]内加入secure_file_priv=/var/lib/mysql
​
linux下:修改my.cnf 在[mysqld]内加入secure_file_priv=/var/lib/mysql

你可能感兴趣的:(补充,docker,mysql,adb)