一、 前期准备工作,创建配置文件目录,log文件目录,数据库DATA和WEB站点目录
[root@T1 ~]# mkdir -p /lnmp/conf/{mysql,nginx,php} /lnmp/logs/{mysql,nginx,php} /lnmp/data/{mysql,www}
[root@T1 ~]# tree /lnmp/
/lnmp/
├── conf
│ ├── mysql mysql配置文件
│ ├── nginx nginx配置文件
│ └── php php配置文件
├── data
│ ├── mysql MySQL数据文件目录
│ └── www 站点根目录
└── logs
├── mysql mysql日志文件
├── nginx nginx日志文件
└── php php日志文件
``````````````
2. 安装mysql5.7镜像
2.1 目录映射关系[镜像---->主机]
/etc/mysql/mysql.conf.d ---->/lnmp/conf/mysql
/var/lib/mysql ---->/lnmp/data/mysql
/var/log/mysql ---->/lnmp/logs/mysql
2.2 下载镜像
[root@T1 ~]# docker pull mysql:5.7
创建一个容器
[root@T1 image]# docker run -d -v /lnmp/conf/mysql:/etc/mysql/mysql.conf.d -v /lnmp/data/mysql:/var/lib/mysql -v /lnmp/logs/mysql:/var/log/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name dnmp_mysql5.7 mysql:5.7
0b7e685c3cbc3aa838230de31e0f1ddb37ce2e7a058c533648729183e39068eb
[root@T1 image]# netstat -nlp|grep 3306
tcp6 0 0 :::3306 :::* LISTEN 6700/docker-proxy
[root@T1 image]# docker ps |grep dnmp_mysql5.7
0b7e685c3cbc mysql:5.7 "docker-entrypoint.s…" 47 seconds ago Up 46 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp dnmp_mysql5.7
2.3 目录关系映射后,log的属主和属主都变了,需要改变为原来的属性[日志目录修改属主和属组为mysql:adm],不要问我为什么
[root@T1 image]# docker exec -it dnmp_mysql5.7 /bin/bash -c "ls -l /var/log/ |grep mysql"
drwxr-xr-x 2 root root 6 Jan 9 21:40 mysql
[root@T1 image]# docker exec -it dnmp_mysql5.7 /bin/bash -c "chown mysql:adm /var/log/mysql/ -R"
[root@T1 image]# docker exec -it dnmp_mysql5.7 /bin/bash -c "ls -l /var/log/ |grep mysql"
drwxr-xr-x 2 mysql adm 6 Jan 9 21:40 mysql
2.4 创建mysql的配置文件,下面配置只是测试日志路径,优化配置还需要完善
# cat >> /lnmp/conf/mysql/my.cnf << EOF
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
max_connections = 1000
symbolic-links=0
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
slow_query_log =1
long_query_time = 10
slow_query_log_file = /var/log/mysql/slow.log
log-error=/var/log/mysql/error.log
server-id =100
log_bin=/var/log/mysql/mysql-bin
EOF
2.5 重启容器
[root@T1 lnmp]# docker restart 0b7e685c3cbc
[root@T1 lnmp]# docker exec -it dnmp_mysql5.7 /bin/bash -c "ln -s /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock"
-------------------------------------------------------------------------------------------------------------------------->
如果登陆数据库报如下错误
root@0b7e685c3cbc:/# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root@0b7e685c3cbc:/# ln -s /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock
-------------------------------------------------------------------------------------------------------------------------->
2.6 查看目录映射关系,已经OK了
[root@T1 lnmp]# ls /lnmp/logs/mysql/ /lnmp/conf/mysql/ /lnmp/data/mysql/
/lnmp/conf/mysql/:
my.cnf
/lnmp/data/mysql/:
0b7e685c3cbc.pid ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 mysql.sock performance_schema public_key.pem server-key.pem
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql mysql.sock.lock private_key.pem server-cert.pem sys
/lnmp/logs/mysql/:
error.log mysql-bin.000001 mysql-bin.index slow.log
就可以解决问题了
我们的my.cnf也已经生效了,数据库容器创建完成
mysql> show variables like '%max_connections%';
mysql> show variables like '%slow_query_log%';
max_connections 1000
slow_query_log ON
slow_query_log_file /var/log/mysql/slow.log
--------------------------------------------------------------------------------------------------->