【问题标题】:How to set up root password while installing mysql in docker?在docker中安装mysql时如何设置root密码?
【发布时间】:2021-11-17 07:08:43
【问题描述】:

我正在尝试在 docker 中安装 mysql,并使用以下 Dockerfile 将 root 的默认密码设置为 root。

Dockerfile:

FROM ubuntu:20.04

COPY mysql_setup.sh /software/mysql_setup.sh
RUN sh /software/mysql_setup.sh

CMD ["/bin/sh"]

在哪里/software/mysql_setup.sh

# Download and Install the Latest Updates for the OS
apt-get update && apt-get upgrade -y

# Set the Server Timezone to CST
echo "America/Chicago" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata

# Enable Ubuntu Firewall and allow SSH & MySQL Ports
ufw enable
ufw allow 22
ufw allow 3306

# Install essential packages
apt-get -y install zsh htop

# Install MySQL Server in a Non-Interactive mode. Default root password will be "root"
echo "mysql-server mysql-server-8.0.26/root_password password root" | sudo debconf-set-selections
echo "mysql-server mysql-server-8.0.26/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mysql-server


# Run the MySQL Secure Installation wizard
mysql_secure_installation

sed -i 's/127\.0\.0\.1/0\.0\.0\.0/g' /etc/mysql/my.cnf
mysql -uroot -p -e 'USE mysql; UPDATE `user` SET `Host`="%" WHERE `User`="root" AND `Host`="localhost"; DELETE FROM `user` WHERE `Host` != "%" AND `User`="root"; FLUSH PRIVILEGES;'

service mysql restart

但上面的代码似乎无法正常工作,因为当我进入 docker 并使用 root 作为密码时,它会出错。

root@839f2946bfdf:/# mysql -u root -p 
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

我该如何解决这个错误?

【问题讨论】:

    标签: mysql docker passwords


    【解决方案1】:

    您不能以正常方式在容器内运行 mysql 安装。像ufw 这样的事情是做不到的。整个RUN 步骤仅在构建时运行,因此在容器启动时service mysql restart 不会运行,只有CMD 会运行。

    建议只使用 Docker 库 mysql 容器。有一个标准的 MYSQL_ROOT_PASSWORD 变量用于设置 root 密码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 2015-07-10
      • 2016-11-16
      • 2018-07-05
      • 2015-11-01
      相关资源
      最近更新 更多