add mysql port variable to script

This commit is contained in:
Daniel Trostli
2025-10-09 13:36:14 -04:00
committed by James Loh
parent a640127945
commit 275a59fc25

View File

@@ -106,12 +106,13 @@ EOF
validate_mysql_connection() { validate_mysql_connection() {
local host=$1 local host=$1
local database=$2 local database=$2
local user=$3 local port=$3
local password=$4 local user=$4
local password=$5
echo "Testing MySQL connection..." echo "Testing MySQL connection..."
if mysql -h"$host" -u"$user" -p"$password" -e "SELECT 1 FROM information_schema.tables WHERE table_schema='$database' LIMIT 1;" &>/dev/null; then if mysql -h"$host" -u"$user" -p"$password" -P"$port" -e "SELECT 1 FROM information_schema.tables WHERE table_schema='$database' LIMIT 1;" &>/dev/null; then
echo "✓ MySQL connection successful" echo "✓ MySQL connection successful"
return 0 return 0
else else
@@ -205,11 +206,12 @@ migrate_content() {
test_mysql_dump() { test_mysql_dump() {
local host=$1 local host=$1
local database=$2 local database=$2
local user=$3 local port=$3
local password=$4 local user=$4
local password=$5
# Try a minimal dump to test permissions # Try a minimal dump to test permissions
if MYSQL_PWD="$password" mysqldump --no-tablespaces --no-data -h"$host" -u"$user" "$database" >/dev/null 2>&1; then if MYSQL_PWD="$password" mysqldump --no-tablespaces --no-data -P"$port" -h"$host" -u"$user" "$database" >/dev/null 2>&1; then
return 0 return 0
else else
return 1 return 1
@@ -220,15 +222,21 @@ test_mysql_dump() {
migrate_database() { migrate_database() {
local mysql_host local mysql_host
local mysql_database local mysql_database
local mysql_port
mysql_host=$(jq -r < "${current_location}/config.production.json" '.database.connection.host') mysql_host=$(jq -r < "${current_location}/config.production.json" '.database.connection.host')
mysql_database=$(jq -r < "${current_location}/config.production.json" '.database.connection.database') mysql_database=$(jq -r < "${current_location}/config.production.json" '.database.connection.database')
mysql_port=$(jq -r < "${current_location}/config.production.json" '.database.connection.port')
# Default to 3306 if port is missing or null
if [[ -z "$mysql_port" || "$mysql_port" == "null" ]]; then
mysql_port=3306
fi
echo "Exporting database from $mysql_host..." echo "Exporting database from $mysql_host..."
# Export database with proper error handling # Export database with proper error handling
local dump_output local dump_output
local dump_status local dump_status
dump_output=$(MYSQL_PWD="$mysql_password" mysqldump --no-tablespaces -h"$mysql_host" -u"$mysql_user" "$mysql_database" 2>&1 > "$TEMP_SQL_FILE") dump_output=$(MYSQL_PWD="$mysql_password" mysqldump --no-tablespaces -h"$mysql_host" -u"$mysql_user" -P"$mysql_port" "$mysql_database" 2>&1 > "$TEMP_SQL_FILE")
dump_status=$? dump_status=$?
# Check for errors in output (mysqldump may return 0 even with some errors) # Check for errors in output (mysqldump may return 0 even with some errors)
@@ -413,10 +421,16 @@ main() {
# Get database configuration # Get database configuration
local mysql_host local mysql_host
local mysql_database local mysql_database
local mysql_port
local ghost_mysql_user local ghost_mysql_user
local ghost_mysql_password local ghost_mysql_password
mysql_host=$(jq -r < "${current_location}/config.production.json" '.database.connection.host') mysql_host=$(jq -r < "${current_location}/config.production.json" '.database.connection.host')
mysql_database=$(jq -r < "${current_location}/config.production.json" '.database.connection.database') mysql_database=$(jq -r < "${current_location}/config.production.json" '.database.connection.database')
mysql_port=$(jq -r < "${current_location}/config.production.json" '.database.connection.port')
# Default to 3306 if port is missing or null
if [[ -z "$mysql_port" || "$mysql_port" == "null" ]]; then
mysql_port=3306
fi
ghost_mysql_user=$(jq -r < "${current_location}/config.production.json" '.database.connection.user') ghost_mysql_user=$(jq -r < "${current_location}/config.production.json" '.database.connection.user')
ghost_mysql_password=$(jq -r < "${current_location}/config.production.json" '.database.connection.password') ghost_mysql_password=$(jq -r < "${current_location}/config.production.json" '.database.connection.password')
@@ -454,7 +468,7 @@ main() {
# Try Ghost's own credentials first # Try Ghost's own credentials first
echo "Testing database export with Ghost's credentials..." echo "Testing database export with Ghost's credentials..."
if test_mysql_dump "$mysql_host" "$mysql_database" "$ghost_mysql_user" "$ghost_mysql_password"; then if test_mysql_dump "$mysql_host" "$mysql_database" "$mysql_port" "$ghost_mysql_user" "$ghost_mysql_password"; then
echo "✓ Ghost's credentials have sufficient privileges" echo "✓ Ghost's credentials have sufficient privileges"
mysql_user="$ghost_mysql_user" mysql_user="$ghost_mysql_user"
mysql_password="$ghost_mysql_password" mysql_password="$ghost_mysql_password"
@@ -473,7 +487,7 @@ main() {
echo "" echo ""
# Validate connection # Validate connection
if ! validate_mysql_connection "$mysql_host" "$mysql_database" "$mysql_user" "$mysql_password"; then if ! validate_mysql_connection "$mysql_host" "$mysql_database" "$mysql_port" "$mysql_user" "$mysql_password"; then
echo "" echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Could not connect to MySQL database." echo "Could not connect to MySQL database."
@@ -488,7 +502,7 @@ main() {
fi fi
# Test dump permissions # Test dump permissions
if ! test_mysql_dump "$mysql_host" "$mysql_database" "$mysql_user" "$mysql_password"; then if ! test_mysql_dump "$mysql_host" "$mysql_database" "$mysql_port" "$mysql_user" "$mysql_password"; then
echo "" echo ""
echo "ERROR: The provided user doesn't have sufficient privileges for database export." echo "ERROR: The provided user doesn't have sufficient privileges for database export."
echo "Please ensure the user has the necessary privileges or try a different user." echo "Please ensure the user has the necessary privileges or try a different user."
@@ -632,7 +646,7 @@ main() {
echo "Once you're checked over the migration you can remove the old installation files and database by running:" echo "Once you're checked over the migration you can remove the old installation files and database by running:"
echo "" echo ""
echo " rm -r $current_location/" echo " rm -r $current_location/"
echo " mysql -h$mysql_host -u$mysql_user -p -e 'DROP DATABASE IF EXISTS ${mysql_database}'" echo " mysql -h$mysql_host -u$mysql_user -p -P$mysql_port -e 'DROP DATABASE IF EXISTS ${mysql_database}'"
echo "" echo ""
echo "This will remove the old Ghost CLI and Ghost 5.x installation" echo "This will remove the old Ghost CLI and Ghost 5.x installation"
echo "" echo ""