From 7696cb42db31ca13c57bd669c2f87d30db7e76a9 Mon Sep 17 00:00:00 2001 From: Jiya Gupta Date: Sat, 4 Jan 2025 15:34:44 -0500 Subject: [PATCH 1/2] added script to update hosts for Database Connection Issue While Setting up using Docker --- update_hosts.sh | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 update_hosts.sh diff --git a/update_hosts.sh b/update_hosts.sh new file mode 100644 index 0000000000..18cad09897 --- /dev/null +++ b/update_hosts.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# Get the MySQL container ID +MYSQL_CONTAINER_ID=$(docker ps --format '{{.ID}} {{.Names}}' | grep 'wikiedudashboard-mysql-1' | awk '{print $1}') + +echo $MYSQL_CONTAINER_ID + +# Fetch the IP address of the MySQL container +MYSQL_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$MYSQL_CONTAINER_ID") + +# Check if we successfully fetched the IP address +if [ -z "$MYSQL_IP" ]; then + echo "Failed to retrieve the MySQL container IP address. Please ensure the Docker containers are running by executing 'docker compose up -d'." + if grep -q "$MYSQL_IP mysql" /etc/hosts; then + sudo sed -i '/[[:space:]]mysql$/d' /etc/hosts + echo "MySQL entry removed from /etc/hosts." + fi + + exit 1 +fi + +# Print the IP address +echo "MySQL IP address: $MYSQL_IP" + +# Backup /etc/hosts file +sudo cp /etc/hosts /etc/hosts.bak + +# Add MySQL container IP to /etc/hosts +if grep -q "$MYSQL_IP mysql" /etc/hosts; then + echo "MySQL entry already exists in /etc/hosts." + # Remove existing MySQL entry from /etc/hosts (if it exists) + sudo sed -i '/[[:space:]]mysql$/d' /etc/hosts + echo "Removed old MySQL entry from /etc/hosts." +fi + +# Add new MySQL container IP address to /etc/hosts +echo "Adding MySQL container IP address to /etc/hosts" +echo "$MYSQL_IP mysql" | sudo tee -a /etc/hosts > /dev/null + +# Print success message +echo "MySQL IP address has been added to /etc/hosts." \ No newline at end of file From 29e251a856240c99702ced155cce831656a63f90 Mon Sep 17 00:00:00 2001 From: Jiya Gupta Date: Sat, 4 Jan 2025 15:39:29 -0500 Subject: [PATCH 2/2] added script info in docker docs --- docs/docker.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/docker.md b/docs/docker.md index 1721bcb48c..67ffabc9fa 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -58,3 +58,10 @@ If you are using Linux you may encounter permission error when trying to change $ sudo chown $USER:$GROUPS -R ./ ``` The command will change the owner of all file inside project directory to the `$USER:$GROUPS` + +If you encounter the following error when connecting to the database: _"There is an issue connecting with your hostname mysql"_, please run the following command: +```sh +bash ./update_hosts.sh +``` +This script automatically resolves the issue by ensuring proper network mapping in your system's ```/etc/hosts``` file, facilitating seamless database connections. It retrieves the IP address of the MySQL Docker container, checks for any existing hostname entries, and updates or removes outdated ones. This process ensures that the hostname ```mysql``` is correctly mapped to the container’s IP address. +This bash script is designed for Linux distributions like Fedora.