Database creation finally working

This commit is contained in:
Shadchamp
2025-08-28 13:22:14 -04:00
parent 252a38b566
commit 9306a1492e
3 changed files with 27 additions and 19 deletions
+3 -1
View File
@@ -15,7 +15,6 @@ RUN pacman -Sy --noconfirm archlinux-keyring \
valkey \
nginx \
python3 \
python-mysql-connector \
php \
php-fpm \
php-gd \
@@ -34,6 +33,9 @@ RUN pacman -Sy --noconfirm archlinux-keyring \
# Set up MariaDB
RUN mkdir -p /run/mysqld && chown mysql:mysql /run/mysqld
# Install bun globally using npm
RUN npm install -g bun || npm update -g bun
# Enable required PHP extensions
RUN sed -i '/^;zend_extension=opcache/s/^;//' /etc/php/php.ini \
&& sed -i '/^;extension=iconv/s/^;//' /etc/php/php.ini \
+24 -17
View File
@@ -1,5 +1,5 @@
import os
import mysql.connector
import subprocess
# Read .env file
env_path = '/var/www/html/.env'
@@ -18,22 +18,29 @@ 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)
# Create database and user
mysql_commands = f"""
CREATE DATABASE IF NOT EXISTS `{db_database}`;
CREATE USER IF NOT EXISTS '{db_username}'@'%' IDENTIFIED BY '{db_password}';
GRANT ALL PRIVILEGES ON `{db_database}`.* TO '{db_username}'@'%';
FLUSH PRIVILEGES;
"""
# 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()
subprocess.run([
"mariadb",
"-u", "root",
"-e", mysql_commands
], check=True)
# Create database
cursor.execute(f"CREATE DATABASE IF NOT EXISTS `{db_database}`;")
print(f"Database '{db_database}' created or already exists.")
# Update root password
cursor.close()
conn.close()
mysql_commands = f"""
ALTER USER 'root'@'localhost' IDENTIFIED BY '{db_password}';
FLUSH PRIVILEGES;
"""
subprocess.run([
"mariadb",
"-u", "root",
"-e", mysql_commands
], check=True)
-1
View File
@@ -28,7 +28,6 @@ if [[ "$1" == "setup" ]]; then
echo "Waiting for MariaDB to be available..."
sleep 2
done
tail -f /dev/null
python3 /database-setup.py
# Keep MariaDB running in the background
php artisan migrate:fresh --seed