SSH隧道连接PythonAnywhere MySQL至Tableau失败:端口与权限问题
Let's break down why your Python code works but Tableau is throwing that access denied error, and fix it step by step.
First: Fix the MySQL Username Format (The Most Common Culprit)
PythonAnywhere uses a specific naming convention for MySQL users: it's your_pythonanywhere_username$your_db_username, not just myusername. For example, if your PythonAnywhere username is jonathanbechtel and your DB user is the default root, your full MySQL username is jonathanbechtel$root.
That's likely why you're seeing Access denied for user 'myusername'@'localhost'—you're probably entering just the short username in Tableau, while your Python code is using the full formatted one (check your info['username'] value, I bet it's the full string).
Second: Ensure Your SSH Tunnel is Properly Configured and Active
For Git Bash:
Your tunnel command is correct, but a few things to check:
- Keep the SSH session open: The tunnel only exists while the Git Bash window is running. If you close it, the connection drops.
- Avoid port conflicts: If you have a local MySQL server running on 3306, switch to another port like 3307 to avoid clashes:
ssh -L 3307:jonathanbechtel.mysql.pythonanywhere-services.com:3306 jonathanbechtel@ssh.pythonanywhere.com - Verify the tunnel is working: Run
telnet 127.0.0.1 3306(or 3307) — if it connects, the tunnel is up. TheTIME_WAITstate innetstatjust means a previous connection is in the closing phase; as long as telnet works, the tunnel is usable.
For PuTTY:
If you're using PuTTY for tunneling, double-check these settings:
- Go to Connection > SSH > Tunnels
- Enter
3306(or 3307) as the Source port - Enter
jonathanbechtel.mysql.pythonanywhere-services.com:3306as the Destination - Select Local and Auto, then click Add
- Log into your PythonAnywhere SSH session and keep the PuTTY window open
Third: Configure Tableau Correctly
Once your tunnel is active, set up Tableau's MySQL connection like this:
- Server:
127.0.0.1(use this instead oflocalhostto avoid DNS resolution issues) - Port: The port you used for your tunnel (3306 or 3307)
- Username: The full PythonAnywhere MySQL username (e.g.,
jonathanbechtel$root) - Password: Your MySQL database password (not your PythonAnywhere SSH password)
- Database: Your full PythonAnywhere database name (e.g.,
jonathanbechtel$mydatabase)
Also, make sure you're using the MySQL ODBC 8.0 Driver (match the bitness to your Tableau installation—64-bit Tableau needs 64-bit drivers, 32-bit needs 32-bit). Avoid the "MySQL Legacy" driver as it can cause compatibility issues.
Why Does the Python Code Work?
Your Python script uses the sshtunnel library which handles the port forwarding correctly, and you're passing the full formatted MySQL username via info['username']. The key difference is that Tableau was missing the full username string.
Quick Validation Steps
To rule out other issues:
- Test the tunnel with MySQL Workbench: Use
127.0.0.1:3306, the full username, and DB password. If it connects, the tunnel and permissions are good. - Test with the MySQL command line:
Enter your DB password—if you can log in, the problem is definitely in Tableau's configuration.mysql -h 127.0.0.1 -P 3306 -u jonathanbechtel$root -p
内容的提问来源于stack exchange,提问作者Jonathan Bechtel

