Score:0

socket connection error while building mysql on ubuntu 20.04 docker image

in flag

I am trying to build mysql on ubuntu 20.04 docker image, using dockerfile and configuration bash script to install and configure mysql.

Dockerfile:

FROM ubuntu:20.04

RUN apt-get update -qq && apt-get upgrade -y 

ENV MYSQL_DATABASE: 'test'
ENV MYSQL_USER: 'user1'
ENV MYSQL_PASSWORD: 'xxxxx'
ENV MYSQL_ROOT_PASSWORD: 'root'

COPY . .

RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata

RUN bash configure_mysql.sh

EXPOSE 3306


Configuration Script:

#!/bin/bash

set -e # Exit script immediately on first error.
#set -x # Print commands and their arguments as they are executed.

# Check if MySQL environment is already installed
RUN_ONCE_FLAG=~/.mysql_env_build_time
MYSQL_PASSWORD="root"

if [ -e "$RUN_ONCE_FLAG" ]; then
  echo "MySQL Server environment is already installed."
  exit 0
fi

# Update Ubuntu package index
apt-get update -y -qq


# Installs MySQL 5.5
echo "mysql-server mysql-server/root_password password $MYSQL_PASSWORD" | debconf-set-selections
echo "mysql-server mysql-server/root_password_again password $MYSQL_PASSWORD" | debconf-set-selections
apt-get install -y mysql-server

# Configures MySQL
sed -i 's/127.0.0.1/0.0.0.0/g' /etc/mysql/my.cnf
sed -i '/\[mysqld\]/a\lower_case_table_names=1' /etc/mysql/my.cnf
echo "MySQL Password set to '${MYSQL_PASSWORD}'. Remember to delete ~/.mysql.passwd" | tee ~/.mysql.passwd; 
mysql -u root -p $MYSQL_PASSWORD -e "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '$MYSQL_PASSWORD'; FLUSH PRIVILEGES;";
service mysql restart
121.122.1
# Installs basic dependencies
echo "installing unzip, git and curl"
apt-get install -y unzip git curl

# Configures prompt color
sed -i 's/#force_color_prompt=yes/force_color_prompt=yes/g' ~/.bashrc
echo 'source ~/.bashrc' >> ~/.bash_profile
source ~/.bash_profile

# Cleaning unneeded packages
apt-get autoremove -y
apt-get clean

# sets "run once" flag
date > $RUN_ONCE_FLAG

However, the configuration script (line no. 29) returns the following error:

Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
The command '/bin/sh -c bash configure_mysql.sh' returned a non-zero code: 1

I have tried changing the permission by passing:

chown mysql:mysql /var/run/mysqld/
chmod -R 777 /var/run/mysqld/

but it doesn't work. May I ask, how can i resolver this issue? Looking forward to the suggestions, Thanks!

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.