Files
my-vault/01_Projects/Work/Government-Projects/Industry-Info/Deployment.md
T

32 KiB

e

IP Address Hostname Component Config
10.194.64.102 gzii-db-3 etcd, xtradb cluster
10.194.64.103 gzii-db-4 xtradb cluster
10.194.64.104 gzii-app-3 proxysql,haproxy

docker warn

modprobe br_netfilter
echo "br_netfilter" | tee -a /etc/modules-load.d/br_netfilter.conf
sysctl -w net.bridge.bridge-nf-call-iptables=1
sysctl -w net.bridge.bridge-nf-call-ip6tables=1

Proxysql

gzii-app-3

proxyuser password

o{Wk45wMsckwsowz

create-proxysql.sh


#!/bin/bash

# Configuration settings
export CLUSTER_NAME="gzii-2"
export ETCD_HOST="10.194.64.102"
export DISCOVERY_SERVICE="${ETCD_HOST}:2379"
export MYSQL_ROOT_PASSWORD="wc97fjvDg:Ywgyad"
export MYSQL_PROXY_USER="jmwrapid"
export MYSQL_PROXY_PASSWORD="Passw0rd@2024"
export DATA_DIR="./data"

# Ensure the 'data' directory exists and has the correct ownership
if [ ! -d "$DATA_DIR" ]; then
  echo "Directory '$DATA_DIR' does not exist. Creating it..."
  mkdir -p "$DATA_DIR"
fi

# Ensure the directory owner is '1001:1001'
echo "Setting ownership of '$DATA_DIR' to user 1001 and group 1001..."
chown 1001:1001 "$DATA_DIR"

# Check if the container is already running
if docker ps --filter "name=proxysql" --format '{{.Names}}' | grep -q "proxysql"; then
  echo "Container 'proxysql' is already running."
  exit 0
fi

# Check if the container exists but is stopped
if docker ps -a --filter "name=proxysql" --format '{{.Names}}' | grep -q "proxysql"; then
  echo "Container 'proxysql' exists but is stopped. Starting it..."
  docker start proxysql
  exit 0
fi

# Run the ProxySQL container with environment variables (without monitor user)
echo "Starting ProxySQL container..."

docker run -d \
  --add-host=gzii-db-3:10.194.64.102 \
  --add-host=gzii-db-4:10.194.64.103 \
  --name=proxysql \
  --net host \
  -e CLUSTER_NAME=$CLUSTER_NAME \
  -e ETCD_HOST=$ETCD_HOST \
  -e DISCOVERY_SERVICE=$DISCOVERY_SERVICE \
  -e MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD \
  -e MYSQL_PROXY_USER=$MYSQL_PROXY_USER \
  -e MYSQL_PROXY_PASSWORD=$MYSQL_PROXY_PASSWORD \
  -v $(pwd)/data:/var/lib/proxysql:rw \
  -v $(pwd)/config/proxysql-admin.cnf:/etc/proxysql-admin.cnf:rw \
  -v $(pwd)/config/create_proxysql_login_file.sh:/var/lib/proxysql/create_proxysql_login_file.sh \
  perconalab/proxysql2:2.7.1-1.2

# Check if the container started successfully
if docker ps --filter "name=proxysql" --format '{{.Names}}' | grep -q "proxysql"; then
  echo "ProxySQL container started successfully."
else
  echo "Failed to start ProxySQL container."
fi

setup.sh

#!/bin/bash

# Configuration Variables
CONTAINER_NAME="proxysql"
PXC_HOSTS=("gzii-db-3" "gzii-db-4") # Backend nodes
WRITER_NODE="gzii-db-4" # Define the writer node
APP_PROXY_USER="jmwrapid"
APP_PROXY_PASSWORD="Passw0rd@2024"
PXC_ROOTPASS='wc97fjvDg:Ywgyad'
PROXYSQL_ADMIN_USER="admin"
PROXYSQL_ADMIN_PASSWORD="admin"
APP_PROXY_HOSTGROUP=21 # Application proxy hostgroup
READER_HOSTGROUP=10    # Default reader hostgroup
WRITER_HOSTGROUP=11    # Default writer hostgroup
BACKUP_WRITER_HOSTGROUP=12 # Backup writer hostgroup
MONITOR_USER="monitor"
MONITOR_PASSWORD="monitor"

# Log function
log() {
  echo "$(date +'%Y-%m-%d %H:%M:%S') - $1"
}

# Utility function for executing SQL commands in ProxySQL
execute_sql() {
  local query="$1"
  log "Executing SQL: $query"
  docker exec -i $CONTAINER_NAME mysql -u $PROXYSQL_ADMIN_USER -p$PROXYSQL_ADMIN_PASSWORD -h 127.0.0.1 -P6032 -e "$query"
}

# Step 1: Create Application Proxy User in PXC Cluster
create_app_proxy_user_in_pxc() {
  log "Creating application proxy user in the PXC cluster..."
  for NODE in "${PXC_HOSTS[@]}"; do
    docker exec -i $CONTAINER_NAME mysql -u root -p$PXC_ROOTPASS -h $NODE -e "
    CREATE USER IF NOT EXISTS '$APP_PROXY_USER'@'%' IDENTIFIED BY '$APP_PROXY_PASSWORD';
    GRANT SELECT, REPLICATION CLIENT ON *.* TO '$APP_PROXY_USER'@'%';
    FLUSH PRIVILEGES;" || {
      log "FAILURE: Could not create user on node $NODE!"
      exit 1
    }
  done
}

# Step 2: Enable ProxySQL Configuration with proxysql-admin
enable_proxysql() {
  log "Configuring ProxySQL with proxysql-admin --enable..."
  docker exec -i $CONTAINER_NAME proxysql-admin --enable \
    --proxysql-username=$PROXYSQL_ADMIN_USER \
    --proxysql-password=$PROXYSQL_ADMIN_PASSWORD \
    --monitor-username=$MONITOR_USER \
    --monitor-password=$MONITOR_PASSWORD \
    --writer-hg=$WRITER_HOSTGROUP \
    --reader-hg=$READER_HOSTGROUP \
    --backup-writer-hg=$BACKUP_WRITER_HOSTGROUP \
    --cluster-username=root \
    --cluster-password=$PXC_ROOTPASS \
    --offline-hg=13 --debug || {
      log "FAILURE: proxysql-admin --enable failed!"
      exit 1
    }
}

# Step 3: Create Custom Hostgroup for App Proxy User
create_custom_hostgroup() {
  log "Ensuring hostgroup $APP_PROXY_HOSTGROUP is properly configured..."
  for NODE in "${PXC_HOSTS[@]}"; do
    execute_sql "
      DELETE FROM mysql_servers WHERE hostgroup_id=$APP_PROXY_HOSTGROUP AND hostname='$NODE';
      INSERT INTO mysql_servers (hostgroup_id, hostname, port, status)
      VALUES ($APP_PROXY_HOSTGROUP, '$NODE', 3306, 'ONLINE');
      LOAD MYSQL SERVERS TO RUNTIME;
      SAVE MYSQL SERVERS TO DISK;"
  done
}

# Step 4: Sync Users with proxysql-admin
sync_users() {
  log "Syncing users with proxysql-admin --syncusers..."
  docker exec -i $CONTAINER_NAME proxysql-admin --syncusers \
    --proxysql-username=$PROXYSQL_ADMIN_USER \
    --proxysql-password=$PROXYSQL_ADMIN_PASSWORD \
    --cluster-username=root \
    --cluster-password=$PXC_ROOTPASS \
    --monitor-username=$MONITOR_USER \
    --monitor-password=$MONITOR_PASSWORD \
    --debug || {
      log "FAILURE: proxysql-admin --syncusers failed!"
      exit 1
    }
}

# Step 5: Add Application Proxy User to ProxySQL
configure_app_proxy_user() {
  log "Configuring application proxy user in ProxySQL..."
  execute_sql "
    INSERT OR REPLACE INTO mysql_users (username, password, default_hostgroup, active)
    VALUES ('$APP_PROXY_USER', '$APP_PROXY_PASSWORD', $APP_PROXY_HOSTGROUP, 1);
    LOAD MYSQL USERS TO RUNTIME;
    SAVE MYSQL USERS TO DISK;"
}

# Step 6: Verify Configuration
verify_configuration() {
  log "Verifying ProxySQL configuration..."
  execute_sql "SHOW MYSQL SERVERS;"
  execute_sql "SHOW MYSQL USERS;"
}

# Step 7: Test Application Proxy User Connectivity
test_connectivity() {
  log "Testing application proxy user connectivity directly to backend nodes..."
  for NODE in "${PXC_HOSTS[@]}"; do
    log "Testing connection to $NODE..."
    docker exec -it $CONTAINER_NAME mysql -u $APP_PROXY_USER -p"$APP_PROXY_PASSWORD" -h "$NODE" -P3306 -e "SELECT 1;" || {
      log "FAILURE: Connection to $NODE failed!"
      exit 1
    }
  done

  log "Testing application proxy user connectivity through ProxySQL..."
  docker exec -it $CONTAINER_NAME mysql -u $APP_PROXY_USER -p"$APP_PROXY_PASSWORD" -h 127.0.0.1 -P3306 -e "SELECT 1;" || {
    log "FAILURE: Connection through ProxySQL failed!"
    exit 1
  }
}

# Main Setup Steps
log "Starting ProxySQL setup script..."
create_app_proxy_user_in_pxc
enable_proxysql
create_custom_hostgroup
sync_users
configure_app_proxy_user
verify_configuration
test_connectivity
log "Setup and testing completed successfully!"


cleanup_proxysql.sh

#!/bin/bash

# Configuration settings
PROXYSQL_CONTAINER="proxysql"         # ProxySQL container name
DATA_DIR="$(pwd)/data"                # Path to the data directory

# Function to ask for confirmation
ask_confirmation() {
    read -p "Are you sure you want to stop and remove the ProxySQL container, and clean the data directory? (y/N): " confirmation
    case "$confirmation" in
        [yY] | [yY][eE][sS])
            echo "Proceeding with cleanup..."
            return 0
            ;;
        *)
            echo "Cleanup aborted."
            exit 0
            ;;
    esac
}

# Function to stop and remove the ProxySQL container
stop_and_remove_container() {
    echo "Stopping and removing ProxySQL container..."
    if docker ps -a | grep -q "$PROXYSQL_CONTAINER"; then
        docker stop "$PROXYSQL_CONTAINER"
        docker rm "$PROXYSQL_CONTAINER"
        echo "ProxySQL container stopped and removed."
    else
        echo "ProxySQL container '$PROXYSQL_CONTAINER' is not running or does not exist."
    fi
}

# Function to clean up the data directory
clean_data_directory() {
    echo "Cleaning data directory '$DATA_DIR'..."
    if [ -d "$DATA_DIR" ]; then
        rm -rf "$DATA_DIR"/*
        echo "Data directory cleaned."
    else
        echo "Data directory '$DATA_DIR' does not exist."
    fi
}

# Main execution
ask_confirmation
stop_and_remove_container
clean_data_directory

echo "Cleanup completed successfully."

check.sh

#!/bin/bash

# Configuration settings
PROXYSQL_CONTAINER="proxysql"          # ProxySQL container name
PROXYSQL_ADMIN_USER="admin"            # ProxySQL admin user
PROXYSQL_ADMIN_PASSWORD="admin"        # ProxySQL admin password
PROXYSQL_PORT=6032                     # ProxySQL Admin port
DISCOVERY_SERVICE="http://10.194.64.102:2379"   # Discovery service (etcd) URL
CLUSTER_NAME="pxc-cluster"             # Cluster name

# Function to check if ProxySQL container is running
check_proxysql_running() {
    echo "Checking if ProxySQL container '$PROXYSQL_CONTAINER' is running..."
    if ! docker ps | grep -q "$PROXYSQL_CONTAINER"; then
        echo "ERROR: ProxySQL container '$PROXYSQL_CONTAINER' is not running."
        exit 1
    fi
    echo "ProxySQL container '$PROXYSQL_CONTAINER' is running."
}

# Function to check ProxySQL status using proxysql-admin
check_proxysql_status() {
    echo "Checking ProxySQL status using proxysql-admin tool..."

    # Execute proxysql-admin to check the status of ProxySQL
    docker exec -it "$PROXYSQL_CONTAINER" /usr/bin/proxysql-admin check

    if [ $? -eq 0 ]; then
        echo "ProxySQL status check completed successfully."
    else
        echo "ERROR: ProxySQL status check failed."
        exit 1
    fi
}

# Function to check the cluster status from discovery service
check_cluster_status() {
    echo "Checking cluster status via discovery service ($DISCOVERY_SERVICE)..."
    
    # Check if the cluster is available in the discovery service (etcd)
    response=$(curl -s "$DISCOVERY_SERVICE/v2/keys/pxc-cluster/$CLUSTER_NAME/")
    
    if echo "$response" | grep -q '"node"'; then
        echo "Cluster '$CLUSTER_NAME' found in discovery service."
    else
        echo "ERROR: Cluster '$CLUSTER_NAME' not found in discovery service."
        exit 1
    fi
}

# Function to check MySQL server status in ProxySQL using SQL query
check_mysql_server_status() {
    echo "Checking MySQL server status in ProxySQL..."

    # Query ProxySQL admin interface for MySQL server status
    docker exec -it "$PROXYSQL_CONTAINER" mysql -u$PROXYSQL_ADMIN_USER -p$PROXYSQL_ADMIN_PASSWORD -h127.0.0.1 -P$PROXYSQL_PORT -e "SELECT * FROM mysql_servers;"

    if [ $? -eq 0 ]; then
        echo "MySQL servers are listed and operational in ProxySQL."
    else
        echo "ERROR: Failed to query MySQL server status in ProxySQL."
        exit 1
    fi
}

# Main execution

# Check if ProxySQL container is running
check_proxysql_running

# Check the ProxySQL status using proxysql-admin
check_proxysql_status

# Check the MySQL server status in ProxySQL using SQL
check_mysql_server_status

# Check the cluster status from the discovery service
check_cluster_status

echo "ProxySQL and Cluster status checks completed successfully."

proxysql-admin.cnf

# proxysql admin interface credentials.
export PROXYSQL_DATADIR='/var/lib/proxysql'
export PROXYSQL_USERNAME='admin'
export PROXYSQL_PASSWORD='admin'
export PROXYSQL_HOSTNAME='localhost'
export PROXYSQL_PORT='6032'

# PXC admin credentials for connecting to pxc-cluster-node.
# Note: Verify if you are using X Protocol (33060) or regular MySQL protocol (3306)
export CLUSTER_USERNAME='operator'   # Cluster admin username, change if needed
export CLUSTER_PASSWORD='operator'   # Cluster admin password, change if needed
export CLUSTER_HOSTNAME='10.194.64.102'
export CLUSTER_PORT='3306'           # Change to 33060 if using X Protocol (MySQL Document Store)

# ProxySQL monitoring user (ProxySQL admin script will create this user in PXC)
export MONITOR_USERNAME='monitor'
export MONITOR_PASSWORD='monitor'
export USE_EXISTING_MONITOR_PASSWORD=1   # Set to 1 if you have an existing monitor user

# Application user to connect to PXC-node through ProxySQL (Set 1 if needed)
export WITH_CLUSTER_APP_USER=0

# ProxySQL hostgroup IDs
export READER_HOSTGROUP_ID='10'
export WRITER_HOSTGROUP_ID='11'
export BACKUP_WRITER_HOSTGROUP_ID='12'
export OFFLINE_HOSTGROUP_ID='13'

# ProxySQL read/write configuration mode.
export MODE="singlewrite"
export WRITE_NODE=""               # Leave empty if you have dynamic writer selection
export WRITERS_ARE_READERS='yes'   # If true, writers are also included in the read hostgroup

# Maximum connections default (used only when inserting a new mysql_servers entry)
export MAX_CONNECTIONS="1000"

# Determines the maximum number of writesets a node can have queued before being shunned
export MAX_TRANSACTIONS_BEHIND=100

# Use STDIN for passing credentials to MySQL client (PSQLADM-282)
export USE_STDIN_FOR_CREDENTIALS=1



proxysql-admin --config-file=/var/lib/proxysql/proxysql-admin.cnf --update-cluster


proxysql-admin --config-file=/var/lib/proxysql/proxysql-admin.cnf --enable
echo "10.194.64.102 gzii-db-3" >> /etc/hosts
echo "10.194.64.103 gzii-db-4" >> /etc/hosts
curl http://gzii-db-3/v2/keys/pxc-cluster/pxc-cluster/?recursive=true | jq

clean.sh

#!/bin/bash

# Configuration variables
PXC_CLUSTER_HOSTS=("10.194.64.102" "10.194.64.103") # List of PXC cluster nodes
MYSQL_ROOT_PASSWORD="your_mysql_root_password"     # MySQL root password (for connecting to PXC cluster)
PROXYSQL_HOSTGROUPS=("10" "11" "12" "13")          # ProxySQL hostgroups (writer, reader, backup, offline)
PROXYSQL_USER="proxyuser"                          # ProxySQL user to remove
PROXYSQL_QUERY_RULES="proxysql%"                   # Pattern for ProxySQL-related query rules

# Step 1: Clean up ProxySQL configuration in PXC cluster
echo "Cleaning up ProxySQL configuration in PXC..."

for NODE in "${PXC_CLUSTER_HOSTS[@]}"; do
    echo "Cleaning up ProxySQL configuration on node $NODE..."
    
    # Remove ProxySQL server entries (for all hostgroups used by ProxySQL)
    for HG in "${PROXYSQL_HOSTGROUPS[@]}"; do
        mysql -h $NODE -uroot -p$MYSQL_ROOT_PASSWORD -e "DELETE FROM mysql_servers WHERE hostgroup_id = $HG;"
    done
    
    # Remove ProxySQL user entries
    mysql -h $NODE -uroot -p$MYSQL_ROOT_PASSWORD -e "DELETE FROM mysql_users WHERE username = '$PROXYSQL_USER';"
    
    # Remove any ProxySQL-related query rules
    mysql -h $NODE -uroot -p$MYSQL_ROOT_PASSWORD -e "DELETE FROM mysql_query_rules WHERE match_pattern LIKE '$PROXYSQL_QUERY_RULES';"
    
    # Commit changes
    mysql -h $NODE -uroot -p$MYSQL_ROOT_PASSWORD -e "LOAD MYSQL SERVERS TO RUNTIME; SAVE MYSQL SERVERS TO DISK;"
    mysql -h $NODE -uroot -p$MYSQL_ROOT_PASSWORD -e "LOAD MYSQL USERS TO RUNTIME; SAVE MYSQL USERS TO DISK;"
    mysql -h $NODE -uroot -p$MYSQL_ROOT_PASSWORD -e "LOAD MYSQL QUERY RULES TO RUNTIME; SAVE MYSQL QUERY RULES TO DISK;"
done

# Step 2: Final confirmation of cleanup
echo "ProxySQL configuration cleanup completed successfully."

CREATE USER 'monitor'@'%' IDENTIFIED BY 'monitor';  
GRANT ALL PRIVILEGES ON *.* TO 'monitor'@'%';
FLUSH PRIVILEGES;
CREATE USER 'operator'@'%' IDENTIFIED BY 'operator';  -- Choose a strong password for the operator user.
GRANT ALL PRIVILEGES ON *.* TO 'operator'@'%'; 
FLUSH PRIVILEGES;

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=33062/tcp --permanent
firewall-cmd --reload
docker exec -it proxysql mysql -u admin -padmin -h 127.0.0.1 -P6032 -e \
"LOAD MYSQL SERVERS TO RUNTIME; \
SAVE MYSQL SERVERS TO DISK; \
LOAD MYSQL USERS TO RUNTIME; \
SAVE MYSQL USERS TO DISK;"

setup.sh


#!/bin/bash

# Configuration Variables
CONTAINER_NAME="proxysql"
PXC_HOSTS=("gzii-db-3" "gzii-db-4") # Backend nodes
WRITER_NODE="gzii-db-4" # Define the writer node
APP_PROXY_USER="jmwrapid"
APP_PROXY_PASSWORD="Passw0rd@2024"
PXC_ROOTPASS='wc97fjvDg:Ywgyad'
PROXYSQL_ADMIN_USER="admin"
PROXYSQL_ADMIN_PASSWORD="admin"
APP_PROXY_HOSTGROUP=21 # Application proxy hostgroup
READER_HOSTGROUP=10    # Default reader hostgroup
WRITER_HOSTGROUP=11    # Default writer hostgroup
BACKUP_WRITER_HOSTGROUP=12 # Backup writer hostgroup
MONITOR_USER="monitor"
MONITOR_PASSWORD="monitor"

# Log function
log() {
  echo "$(date +'%Y-%m-%d %H:%M:%S') - $1"
}

# Utility function for executing SQL commands in ProxySQL
execute_sql() {
  local query="$1"
  log "Executing SQL: $query"
  docker exec -i $CONTAINER_NAME mysql -u $PROXYSQL_ADMIN_USER -p$PROXYSQL_ADMIN_PASSWORD -h 127.0.0.1 -P6032 -e "$query"
}

# Step 1: Create Application Proxy User in PXC Cluster
create_app_proxy_user_in_pxc() {
  log "Creating application proxy user in the PXC cluster..."
  for NODE in "${PXC_HOSTS[@]}"; do
    docker exec -i $CONTAINER_NAME mysql -u root -p$PXC_ROOTPASS -h $NODE -e "
    CREATE USER IF NOT EXISTS '$APP_PROXY_USER'@'%' IDENTIFIED BY '$APP_PROXY_PASSWORD';
    GRANT SELECT, REPLICATION CLIENT ON *.* TO '$APP_PROXY_USER'@'%';
    FLUSH PRIVILEGES;" || {
      log "FAILURE: Could not create user on node $NODE!"
      exit 1
    }
  done
}

# Step 2: Enable ProxySQL Configuration with proxysql-admin
enable_proxysql() {
  log "Configuring ProxySQL with proxysql-admin --enable..."
  docker exec -i $CONTAINER_NAME proxysql-admin --enable \
    --proxysql-username=$PROXYSQL_ADMIN_USER \
    --proxysql-password=$PROXYSQL_ADMIN_PASSWORD \
    --monitor-username=$MONITOR_USER \
    --monitor-password=$MONITOR_PASSWORD \
    --writer-hg=$WRITER_HOSTGROUP \
    --reader-hg=$READER_HOSTGROUP \
    --backup-writer-hg=$BACKUP_WRITER_HOSTGROUP \
    --cluster-username=root \
    --cluster-password=$PXC_ROOTPASS \
    --offline-hg=13 --debug || {
      log "FAILURE: proxysql-admin --enable failed!"
      exit 1
    }
}

# Step 3: Create Custom Hostgroup for App Proxy User
create_custom_hostgroup() {
  log "Ensuring hostgroup $APP_PROXY_HOSTGROUP is properly configured..."
  for NODE in "${PXC_HOSTS[@]}"; do
    execute_sql "
      DELETE FROM mysql_servers WHERE hostgroup_id=$APP_PROXY_HOSTGROUP AND hostname='$NODE';
      INSERT INTO mysql_servers (hostgroup_id, hostname, port, status)
      VALUES ($APP_PROXY_HOSTGROUP, '$NODE', 3306, 'ONLINE');
      LOAD MYSQL SERVERS TO RUNTIME;
      SAVE MYSQL SERVERS TO DISK;"
  done
}

# Step 4: Sync Users with proxysql-admin
sync_users() {
  log "Syncing users with proxysql-admin --syncusers..."
  docker exec -i $CONTAINER_NAME proxysql-admin --syncusers \
    --proxysql-username=$PROXYSQL_ADMIN_USER \
    --proxysql-password=$PROXYSQL_ADMIN_PASSWORD \
    --cluster-username=root \
    --cluster-password=$PXC_ROOTPASS \
    --monitor-username=$MONITOR_USER \
    --monitor-password=$MONITOR_PASSWORD \
    --debug || {
      log "FAILURE: proxysql-admin --syncusers failed!"
      exit 1
    }
}

# Step 5: Add Application Proxy User to ProxySQL
configure_app_proxy_user() {
  log "Configuring application proxy user in ProxySQL..."
  execute_sql "
    INSERT OR REPLACE INTO mysql_users (username, password, default_hostgroup, active)
    VALUES ('$APP_PROXY_USER', '$APP_PROXY_PASSWORD', $APP_PROXY_HOSTGROUP, 1);
    LOAD MYSQL USERS TO RUNTIME;
    SAVE MYSQL USERS TO DISK;"
}

# Step 6: Verify Configuration
verify_configuration() {
  log "Verifying ProxySQL configuration..."
  execute_sql "SHOW MYSQL SERVERS;"
  execute_sql "SHOW MYSQL USERS;"
}

# Step 7: Test Application Proxy User Connectivity
test_connectivity() {
  log "Testing application proxy user connectivity directly to backend nodes..."
  for NODE in "${PXC_HOSTS[@]}"; do
    log "Testing connection to $NODE..."
    docker exec -it $CONTAINER_NAME mysql -u $APP_PROXY_USER -p"$APP_PROXY_PASSWORD" -h "$NODE" -P3306 -e "SELECT 1;" || {
      log "FAILURE: Connection to $NODE failed!"
      exit 1
    }
  done

  log "Testing application proxy user connectivity through ProxySQL..."
  docker exec -it $CONTAINER_NAME mysql -u $APP_PROXY_USER -p"$APP_PROXY_PASSWORD" -h 127.0.0.1 -P3306 -e "SELECT 1;" || {
    log "FAILURE: Connection through ProxySQL failed!"
    exit 1
  }
}

# Main Setup Steps
log "Starting ProxySQL setup script..."
create_app_proxy_user_in_pxc
enable_proxysql
create_custom_hostgroup
sync_users
configure_app_proxy_user
verify_configuration
test_connectivity
log "Setup and testing completed successfully!"



GRANT ALL PRIVILEGES ON jmwrapid.* TO 'jmwrapid'@'%';
FLUSH PRIVILEGES;

corp_appeal_apply

ERROR 1118 (42000) at line 2007: Row size too large (> 8126). Changing some columns to TEXT or BLOB or using ROW_FORMAT=DYNAMIC or ROW_FORMAT=COMPRESSED m$ y help. In current row format, BLOB prefix of 768 bytes is stored inline.

ALTER TABLE corp_appeal_apply ROW_FORMAT=DYNAMIC;

mysql -u root -h 127.0.0.1 -pwc97fjvDg:Ywgyad jmwrapid < jmwrapid-09-Dec-2024.sql

10.194.64.15 mysql root:

Passw0rd@2022

mariadb cluster

root password:

sE1ZV4lVihqETud/2Mw+sA

gzii-db-3:

.env:

# Database credentials
MYSQL_ROOT_PASSWORD=sE1ZV4lVihqETud/2Mw+sA
MYSQL_DATABASE=jmwrapid
MYSQL_USER=appuser
MYSQL_PASSWORD="Passw0rd@2022"

# Cluster configuration
WSREP_CLUSTER_NAME=galera
WSREP_SST_METHOD=rsync
WSREP_CLUSTER_ADDRESS="gcomm://10.194.64.102,10.194.64.103"

bootstrap:

docker run --name gzii-db-3 \
  --network host \
  --env-file .env \
  -v ./data:/var/lib/mysql \
  -v ./config/galera.cnf:/etc/mysql/conf.d/galera.cnf \
  -e WSREP_NODE_ADDRESS=10.194.64.102 \
  -d mariadb:latest \
  --wsrep-new-cluster

config: config/galera.cnf


[mysqld]                                                                                                                                         12:44:37 [2/4913]
log_bin ='/var/log/mysql/mariadb-bin'
binlog_format='row'
expire_logs_days=1
#log_error

# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
default_storage_engine='InnoDB'

# https://mariadb.com/kb/en/set-transaction/
transaction-isolation='READ-COMMITTED'

# https://mariadb.com/docs/reference/mdb/cli/mariadbd/innodb-flush-log-at-trx-commit/
innodb-flush-log-at-trx-commit=1

# https://mariadb.com/docs/reference/mdb/system-variables/innodb_autoinc_lock_mode/
innodb_autoinc_lock_mode=2

# https://www.digitalocean.com/community/tutorials/how-to-change-a-mariadb-data-directory-to-a-new-location-on-centos-7
datadir='/var/lib/mysql'
socket='/run/mysqld/mysqld.sock'

# https://dba.stackexchange.com/questions/130922/error-wsrep-gcs-src-gcs-cppgcs-open1379-failed-to-open-channel-test-clu/131487
[galera]
wsrep_on='ON'
wsrep_provider='/usr/lib/galera/libgalera_smm.so'
wsrep_cluster_address='gcomm://10.194.64.102,10.194.64.103'
wsrep_cluster_name='galera'
wsrep_sst_method='rsync'
wsrep_slave_threads=2
wsrep_node_address='10.194.64.102'

#wsrep_notify_cmd='/etc/mysql/scripts/my-wsrep-notify.sh'
#pxc_strict_mode='PERMISSIVE'

# https://qiita.com/chaspy/items/baad6947ae0f8b169868
wsrep_auto_increment_control='ON'
wsrep_drupal_282555_workaround='ON'
wsrep_retry_autocommit=10

[client]
protocol='TCP'

gzii-db-4

.env

# Database credentials
MYSQL_ROOT_PASSWORD=sE1ZV4lVihqETud/2Mw+sA
MYSQL_DATABASE=jmwrapid
MYSQL_USER=appuser
MYSQL_PASSWORD="Passw0rd@2022"

# Cluster configuration
WSREP_CLUSTER_NAME=galera
WSREP_SST_METHOD=rsync
WSREP_CLUSTER_ADDRESS="gcomm://10.194.64.102,10.194.64.103"

join

docker run --name gzii-db-4 \
  --network host \
  --env-file .env \
  -v ./data:/var/lib/mysql \
  -v ./config/galera.cnf:/etc/mysql/conf.d/galera.cnf \
  -e WSREP_NODE_ADDRESS=10.194.64.103 \
  -d mariadb:latest \
  --wsrep-new-cluster

config: config/galera.cnf


[mysqld]                                                                                                                                         12:44:37 [2/4913]
log_bin ='/var/log/mysql/mariadb-bin'
binlog_format='row'
expire_logs_days=1
#log_error

# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
default_storage_engine='InnoDB'

# https://mariadb.com/kb/en/set-transaction/
transaction-isolation='READ-COMMITTED'

# https://mariadb.com/docs/reference/mdb/cli/mariadbd/innodb-flush-log-at-trx-commit/
innodb-flush-log-at-trx-commit=1

# https://mariadb.com/docs/reference/mdb/system-variables/innodb_autoinc_lock_mode/
innodb_autoinc_lock_mode=2

# https://www.digitalocean.com/community/tutorials/how-to-change-a-mariadb-data-directory-to-a-new-location-on-centos-7
datadir='/var/lib/mysql'
socket='/run/mysqld/mysqld.sock'

# https://dba.stackexchange.com/questions/130922/error-wsrep-gcs-src-gcs-cppgcs-open1379-failed-to-open-channel-test-clu/131487
[galera]
wsrep_on='ON'
wsrep_provider='/usr/lib/galera/libgalera_smm.so'
wsrep_cluster_address='gcomm://10.194.64.102,10.194.64.103'
wsrep_cluster_name='galera'
wsrep_sst_method='rsync'
wsrep_slave_threads=2
wsrep_node_address='10.194.64.103'

#wsrep_notify_cmd='/etc/mysql/scripts/my-wsrep-notify.sh'
#pxc_strict_mode='PERMISSIVE'

# https://qiita.com/chaspy/items/baad6947ae0f8b169868
wsrep_auto_increment_control='ON'
wsrep_drupal_282555_workaround='ON'
wsrep_retry_autocommit=10

[client]
protocol='TCP'

change sql:

sed -i 's/VARCHAR([0-9]*)/TEXT/g' mysqldump.sql
sed -i 's/ROW_FORMAT=COMPACT/ROW_FORMAT=DYNAMIC/g' mysqldump.sql

import:

docker exec -i gzii-db-4 mysql -ujmwrapid -p'Passw0rd@2022' jmwrapid < ../backup/db/jmwrapid-01-Dec-2024.sql

clear mode:

docker exec gzii-db-4 mysql -uroot -p'wc97fjvDg:Ywgyad' -e "SELECT @@GLOBAL.sql_mode;"
mysql> SELECT @@GLOBAL.sql_mode;
+-----------------------------------------------------------------------------------------------------------------------+
| @@GLOBAL.sql_mode                                                                                                     |
+-----------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
CREATE USER 'jmwrapid'@'%' IDENTIFIED BY 'Passw0rd@2024';
GRANT ALL PRIVILEGES ON jmwrapid.* TO 'jmwrapid'@'%';
FLUSH PRIVILEGES;
SHOW GRANTS FOR 'jmwrapid'@'%';


GRANT ALL PRIVILEGES ON jmw.* TO 'jmwrapid'@'%';
FLUSH PRIVILEGES;
SHOW GRANTS FOR 'jmwrapid'@'%';

docker exec -i gzii-db-4 mysql -ujmwrapid -p'Passw0rd@2024' jmwrapid < mysqldump.sql
ORIGINAL_MODE="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
docker exec gzii-db-4 mysql -uroot -p'wc97fjvDg:Ywgyad' -e "SET GLOBAL sql_mode = '$ORIGINAL_MODE';"

redis:

docker run --rm redis:7.4.1 cat /usr/local/etc/redis/redis.conf > ./conf/redis.conf

tomcat:

mkdir -p {conf,logs,webapps}

docker run --name temp-tomcat tomcat:8.5.100-jre8 
docker cp temp-tomcat:/usr/local/tomcat/conf ./ 
docker rm -f temp-tomcat

nginx:

docker run --name temp-nginx nginx:1.27.2 
docker cp temp-nginx:/etc/nginx/conf.d ./conf
docker rm -f temp-nginx

docker run -d --name nginx \
  --restart unless-stopped \
  -p 80:80 \
  -v ./conf:/etc/nginx/conf.d \
  nginx:1.27.2



    access_log /var/log/nginx/access.log main;

    sendfile on;
    keepalive_timeout 65;

    # Define upstreams (formerly HAProxy backends)
    upstream questionnaire_backend {
        server 10.201.11.5:80 max_fails=3 fail_timeout=30s;
    }

    upstream form_api_backend {
        server 10.194.64.104:8180 max_fails=3 fail_timeout=30s;
    }

    server {
        listen 80;
        server_name data.gxj.gz.gov.cn;

        # Location for root path
        location / {
            proxy_pass http://questionnaire_backend/questionnaire/questionnaire/create;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;

            proxy_connect_timeout 30s;
            proxy_read_timeout 60s;
            proxy_send_timeout 60s;
        }

        # Location for /form (static files)
        location /form {
            alias /opt/form/html;
            try_files $uri $uri/ /form/index.html;
        }

        # Location for /form-api/
        location /form-api/ {
            proxy_pass http://form_api_backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;

            proxy_connect_timeout 30s;
            proxy_read_timeout 60s;
            proxy_send_timeout 60s;

            # Handle upstream errors gracefully
            proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
            proxy_redirect default;
            proxy_max_temp_file_size 0k;
        }

        # Optional: Health check endpoint for load balancers
        location /healthcheck {
            return 200 "OK";
            add_header Content-Type text/plain;
        }
    }

CREATE DATABASE IF NOT EXISTS jmwrapid
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_0900_ai_ci;

GRANT ALL PRIVILEGES ON jmwrapid.* TO 'jmwrapid'@'%';

FLUSH PRIVILEGES;

#!/bin/bash

# 配置 MySQL 账号信息(请修改为你的 MySQL 用户名和密码)
MYSQL_USER="jmwrapid"
MYSQL_PASSWORD="Passw0rd@2024"

# 检查是否提供了两个参数
if [ "$#" -ne 2 ]; then
    echo "用法: $0 <数据库名> <SQL文件>"
    exit 1
fi

# 读取输入参数
DB_NAME="$1"
SQL_FILE="$2"

# 检查 SQL 文件是否存在
if [ ! -f "$SQL_FILE" ]; then
    echo "错误: 文件 '$SQL_FILE' 不存在"
    exit 1
fi

# 执行 MySQL 导入
docker exec -i gzii-db-4 mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$DB_NAME" < "$SQL_FILE"

# 检查是否成功
if [ $? -eq 0 ]; then
    echo "✅ SQL 文件 '$SQL_FILE' 已成功导入到数据库 '$DB_NAME'"
else
    echo "❌ 导入失败,请检查数据库和文件是否正确"
fi

etcd

To add a root user in etcd and set its password, you choose the password during the process—there is no default. Here's exactly how to do it in your containerized setup:


Step-by-Step to Add root User in etcd

  1. Set the etcd API version:
export ETCDCTL_API=3

(You can also include it inline if preferred.)


  1. Run the command to add root user:
echo "IeGheikae.Woo5ph" | docker exec -i -e ETCDCTL_API=3 etcd etcdctl --endpoints=http://gzii-db-3:2379 user add root

docker exec -e ETCDCTL_API=3 etcd etcdctl user add root --new-user-password="IeGheikae.Woo5ph"

password:

IeGheikae.Woo5ph

  1. Grant the root user full access:
docker exec etcd etcdctl --endpoints=http://gzii-db-3:2379 role add root

docker exec etcd etcdctl --endpoints=http://gzii-db-3:2379 role grant-permission root --prefix=true readwrite /

docker exec etcd etcdctl --endpoints=http://gzii-db-3:2379 user grant-role root root

  1. Enable auth:
docker exec etcd etcdctl --endpoints=http://gzii-db-3:2379 auth enable

After this, any access to etcd must be done like this:

etcdctl --user root:<YOUR_PASSWORD> --endpoints=http://gzii-db-3:2379 get / --prefix

Let me know when you're ready to test etcd access using the new root credentials.

docker exec -e ETCDCTL_API=3 etcd etcdctl --user=root:IeGheikae.Woo5ph member list