直接上dockerfile代码
1 FROM centos:centos7.9.2009 2 RUN yum install -y wget && \ 3 wget https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm && \ 4 yum -y localinstall mysql80-community-release-el7-11.noarch.rpm && \ 5 rm mysql80-community-release-el7-11.noarch.rpm && \ 6 yum-config-manager --disable mysql80-community && \ 7 yum-config-manager --enable mysql57-community && \ 8 yum -y install mysql-community-server && \ 9 sed -i '/^PIDFile=\/var\/run\/mysqld\/mysqld.pid/s/^/# /' /usr/lib/systemd/system/mysqld.service && \ 10 yum install -y epel-release && \ 11 yum install -y redis && \ 12 sed -i '/^bind 127.0.0.1/s/^/# /' /etc/redis.conf && \ 13 sed -i 's/protected-mode yes/protected-mode no/' /etc/redis.conf && \ 14 yum remove -y wget epel-release && \ 15 yum clean all 16 COPY start.sh /start.sh 17 RUN chmod +x /start.sh 18 ENTRYPOINT ["/start.sh"]
启动脚本
1 #!/bin/bash 2 3 # 检查 MySQL 数据目录是否存在数据文件 4 if [ ! -f /var/lib/mysql/ibdata1 ]; then 5 # 执行 MySQL 初始化 6 mysqld --initialize --explicit_defaults_for_timestamp --user=mysql 7 echo "MySQL initialized!" 8 # 启动 MySQL 服务器 9 echo "Starting MySQL server..." 10 mysqld --user=mysql & 11 12 sleep 5s 13 if grep -q "A temporary password is generated" /var/log/mysqld.log; then 14 # 获取初始密码 15 initial_password=$(grep "A temporary password is generated" /var/log/mysqld.log | awk '{print $NF}') 16 echo "Initial password: $initial_password" 17 18 # 修改密码 19 mysql -uroot -p"$initial_password" --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '1qaz2wsx';" 20 if [ $? -eq 0 ]; then 21 echo "Root password changed successfully!" 22 else 23 echo "Failed to change root password!" 24 exit 1 25 fi 26 mysql -uroot -p1qaz2wsx --connect-expired-password -e <<EOF 27 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;28 FLUSH PRIVILEGES; 29 EOF 30 fi 31 fi 32 33 # 启动 MySQL 服务器 34 echo "Starting MySQL server..." 35 mysqld --user=mysql & 36 37 # 启动 Redis 服务器 38 echo "Starting Redis server..." 39 redis-server /etc/redis.conf 40 41 echo "services started." 42 43 # 持续监听,保持容器运行 44 tail -f /dev/null
生成docker镜像
docker build -t mysql-redis .
启动docker容器
宿主机器的3306端口要转发给容器内的mysql服务监听端口,6379端口要转发给容器内的redis服务端口。
docker run -d --name mysql-redis -p 6379:6379 -p 3306:3306 mysql-redis:latest
启动的docker容器的redis服务已经设置了可以远程连接,mysql远程连接的root用户,密码root
接下去就可以用这个超级用户创建新的数据库了,例如
CREATE DATABASE IF NOT EXISTS new_database DEFAULT CHARSET utf8 COLLATE utf8_general_ci; CREATE USER 'new_user'@'%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON new_database.* TO 'new_user'@'%';
注意把上面数据库名,密码等信息换成实际的
标签:--,redis,yum,&&,mysql,docker,password From: https://www.cnblogs.com/shixiaobin/p/18172221