Skip to content

Commit

Permalink
Merge pull request #6090 from JiyaGupta-cs/update_mysql_hosts
Browse files Browse the repository at this point in the history
 [FIX]: Database Connection Issue by Hostname Resolution During Setup Using Docker
  • Loading branch information
ragesoss authored Jan 6, 2025
2 parents ac19ee0 + 29e251a commit 151fc26
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
41 changes: 41 additions & 0 deletions update_hosts.sh
Original file line number Diff line number Diff line change
@@ -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."

0 comments on commit 151fc26

Please sign in to comment.