docker安装mysql
首先建立好数据卷映射到宿主机的路径:
[root@microservice opt]# mkdir -p /opt/docker/mysql
[root@microservice opt]# mkdir -p /opt/docker/mysql/conf.d
[root@microservice opt]# mkdir -p /opt/docker/mysql/data
创建my.cnf配置文件
[root@microservice opt]# vi /opt/docker/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
然后一条命令搞定mysql:
[root@microservice opt]# docker run -d --name mysql --restart=always -p 3308:3306 -e MYSQL_ROOT_PASSWORD=neIIqlwGsjsfsA6uxNqD -e "TZ=Asia/Shanghai" -v /opt/docker/mysql/data/:/var/lib/mysql -v /opt/docker/mysql/conf.d:/etc/mysql/conf.d -v /opt/docker/mysql/my.cnf:/etc/mysql/my.cnf mysql:5.7 --lower_case_table_names=1
当观察到STATUS为UP状态时,基本上mysql就已经可以使用了:
[root@microservice opt]# docker ps
设置mysql允许远程访问:
[root@microservice]# docker exec -it mysql bash root@7028670f0c2c:/# mysql -uroot -pneIIqlwGsjsfsA6uxNqD mysql> ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'neIIqlwGsjsfsA6uxNqD'; mysql> FLUSH PRIVILEGES;
云服务器开通3308端口后重启就可以使用了!!!
如果需要开启mysql审计日志:
vi my.cnf 增加: log_bin = /var/lib/mysql/mysql_bin server_id =1 general_log_file = /var/log/mysql/mysql.log general_log = 1
建立好数据卷映射到宿主机的审计日志路径:
[root@microservice]# mkdir -p /opt/docker/mysql/logs [root@microservice]# vi mysql.log
docker重新安装mysql:
[root@microservice]# docker stop mysql [root@microservice]# docker rm mysql [root@microservice]# docker run --restart=always --privileged=true -d -v /opt/docker/mysql/data/:/var/lib/mysql -v /opt/docker/mysql/conf.d:/etc/mysql/conf.d -v /opt/docker/mysql/my.cnf:/etc/mysql/my.cnf -v /opt/docker/mysql/logs/mysql.log:/var/log/mysql/mysql.log -p 3306:3306 --name mysql --privileged=true --restart=always -e MYSQL_ROOT_PASSWORD=neIIqlwGsjsfsA6uxNqD -e "TZ=Asia/Shanghai" -d mysql:5.7 --lower_case_table_names=1
mysql导入数据:
docker exec -i mysql kjzsq < /opt/kjzsq.sql
mysql导出数据:
docker exec -it mysql mysqldump -uroot -pneIIqlwGsjsfsA6uxNqD kjzsq > /opt/kjzsq.sql