Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX]: Database Connection Issue by Hostname Resolution During Setup Using Docker #6090

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."
Loading