Mysql database setup

This commit is contained in:
Shadchamp
2025-08-28 13:07:34 -04:00
parent 726aca793e
commit cbf1cf3443
6 changed files with 116 additions and 26 deletions
+1
View File
@@ -0,0 +1 @@
mysql
+2
View File
@@ -14,6 +14,8 @@ RUN pacman -Sy --noconfirm archlinux-keyring \
mariadb \
valkey \
nginx \
python3 \
python-mysql-connector \
php \
php-fpm \
php-gd \
+65
View File
@@ -0,0 +1,65 @@
# UNIT3D-Docker
A Dockerized setup for UNIT3D, an open-source private torrent tracker.
## Features
- Easy deployment with Docker and Docker Compose
- Pre-configured environment for UNIT3D
- Customizable settings
## Prerequisites
- [Docker](https://www.docker.com/get-started)
- [Docker Compose](https://docs.docker.com/compose/)
## Getting Started
1. Clone this repository:
```bash
git clone https://github.com/BillyOutlast/UNIT3D-Docker.git
cd UNIT3D-Docker
```
2. Clone the UNIT3D source code and set up environment variables:
```bash
git clone https://github.com/HDInnovations/UNIT3D.git
cd UNIT3D
cp .env.example .env
```
3. Edit the `.env` file to set a secure database username and password:
```env
DB_USERNAME=your_secure_username
DB_PASSWORD=your_strong_password
```
Replace `your_secure_username` and `your_strong_password` with your own secure values.
4. Build and start the containers:
```bash
docker-compose up -d
```
4. Access UNIT3D at `http://localhost:8000`
## Configuration
- Edit `.env` to customize database, ports, and other settings.
- See `docker-compose.yml` for service definitions.
## Troubleshooting
- Check container logs:
```bash
docker-compose logs
```
- Ensure all required ports are available.
## License
This project is licensed under the MIT License.
## Credits
- [UNIT3D](https://github.com/UNIT3D/UNIT3D)
- Docker Community
+39
View File
@@ -0,0 +1,39 @@
import os
import mysql.connector
# Read .env file
env_path = '/var/www/html/.env'
env_vars = {}
with open(env_path, 'r') as f:
for line in f:
if '=' in line and not line.startswith('#'):
key, value = line.strip().split('=', 1)
env_vars[key] = value
# Get DB info
db_connection = env_vars.get('DB_CONNECTION')
db_host = env_vars.get('DB_HOST')
db_port = int(env_vars.get('DB_PORT', 3306))
db_database = env_vars.get('DB_DATABASE')
db_username = env_vars.get('DB_USERNAME')
db_password = env_vars.get('DB_PASSWORD')
if db_connection != 'mysql':
print("Only MySQL is supported.")
exit(1)
# Connect to MySQL server (not to a specific database)
conn = mysql.connector.connect(
host=db_host,
port=db_port,
user=db_username,
password=db_password
)
cursor = conn.cursor()
# Create database
cursor.execute(f"CREATE DATABASE IF NOT EXISTS `{db_database}`;")
print(f"Database '{db_database}' created or already exists.")
cursor.close()
conn.close()
+1
View File
@@ -21,6 +21,7 @@ services:
- ./unit3d.conf:/etc/nginx/conf.d/unit3d.conf
- ./mysql:/var/lib/mysql
- ./entrypoint.sh:/entrypoint.sh
- ./database-setup.py:/database-setup.py
env_file:
- .env
entrypoint: /entrypoint.sh setup
+8 -26
View File
@@ -6,40 +6,21 @@ if [[ "$1" == "setup" ]]; then
cd /var/www/html
#composer install --no-interaction --prefer-dist
#php artisan key:generate
echo "Please save this to APP_KEY in UNIT3D/.env"
#echo "Please save this to APP_KEY in UNIT3D/.env"
echo "Setting correct permissions for MariaDB data directory..."
sudo chown -R mysql:mysql /var/lib/mysql
sudo chmod 750 /var/lib/mysql
sudo chmod -R 750 /var/lib/mysql
sudo chage -E -1 mysql
echo "Initializing MariaDB system tables if necessary..."
if [ ! -d "/var/lib/mysql/mysql" ]; then
sudo mysql_install_db --basedir=/usr --datadir=/var/lib/mysql
# Extract DB credentials from .env
DB_USER=$(grep '^DB_USERNAME=' /var/www/html/.env | cut -d '=' -f2)
DB_PASS=$(grep '^DB_PASSWORD=' /var/www/html/.env | cut -d '=' -f2)
DB_NAME=$(grep '^DB_DATABASE=' /var/www/html/.env | cut -d '=' -f2)
# Start MariaDB temporarily to create user and database
sudo mysqld_safe --skip-networking &
sleep 5
# Create user, database, and grant privileges
mysql -u root <<EOF
CREATE DATABASE IF NOT EXISTS \`${DB_NAME}\`;
CREATE USER IF NOT EXISTS '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
GRANT ALL PRIVILEGES ON \`${DB_NAME}\`.* TO '${DB_USER}'@'localhost';
FLUSH PRIVILEGES;
EOF
# Stop MariaDB
mysqladmin -u root shutdown
fi
su -s /bin/bash mysql -c "mysql_install_db --basedir=/usr --datadir=/var/lib/mysql"
fi
echo "Starting MariaDB..."
# Prevent tail from running on /dev/null, use a log file instead
# Example: tail MariaDB log file to keep container alive and show logs
su -s /bin/bash mysql -c "mysqld &"
# Wait for MariaDB to be ready
@@ -47,8 +28,9 @@ EOF
echo "Waiting for MariaDB to be available..."
sleep 2
done
tail -f /dev/null
python3 /database-setup.py
# Keep MariaDB running in the background
tail -f /dev/null
php artisan migrate:fresh --seed
npm install -g bun || npm update -g bun
#npm install