#!/bin/bash
# MySQL Remote Access Setup Script
# Run this on your MySQL server (192.168.29.101)

echo "=== MySQL Remote Access Setup ==="

# 1. Backup current configuration
cp /etc/mysql/my.cnf /etc/mysql/my.cnf.backup.$(date +%Y%m%d_%H%M%S)

# 2. Add remote access configuration
cat >> /etc/mysql/my.cnf << 'EOF'

# Remote Access Configuration
[mysqld]
bind-address = 0.0.0.0
wait_timeout = 600
interactive_timeout = 600
connect_timeout = 60
max_allowed_packet = 64M
max_connections = 200
skip-name-resolve
port = 3309
EOF

# 3. Restart MySQL
systemctl restart mysql

# 4. Create remote user (you'll need to enter MySQL root password)
mysql -u root -p << 'EOF'
CREATE USER 'root'@'192.168.29.%' IDENTIFIED BY '';
GRANT ALL PRIVILEGES ON sb_pms.* TO 'root'@'192.168.29.%';
GRANT ALL PRIVILEGES ON dr_shilpi_log_activities.* TO 'root'@'192.168.29.%';
FLUSH PRIVILEGES;
SELECT User, Host FROM mysql.user WHERE User = 'root';
EOF

echo "Setup complete! Test the connection from your client machine."
