Android连接本地MySQL数据库(VertrigoServ)授权无效崩溃问题求助
Hey there, let's tackle that frustrating "Invalid authorization" error and app crash you're dealing with. I've seen this exact issue a bunch of times when connecting Android apps to local MySQL setups, so let's break down the most likely fixes step by step:
1. Double-Check MySQL User Permissions & Credentials
- First off, make sure the username and password you're using in your Android code exactly match what's configured in your VertrigoServ MySQL instance. Vertrigo sometimes uses a default
rootuser with an empty password, but if you set a custom password, don't forget it! - A super common gotcha: MySQL's default
rootuser is often restricted to onlylocalhost(your laptop itself) access. Since your Android device is connecting over the network, you need to grant external access to your user. Run these commands in MySQL (you can use Vertrigo's phpMyAdmin or a MySQL client):
TheGRANT ALL PRIVILEGES ON *.* TO 'your_username'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION; FLUSH PRIVILEGES;%allows access from any IP—for better security, you can replace it with your Android device's specific local IP address once you get it working.
2. Verify VertrigoServ MySQL Configuration
- Open your VertrigoServ MySQL config file (usually
my.iniin themysqlfolder of your Vertrigo installation). Look for thebind-addressline:- If it's set to
127.0.0.1, only your laptop can connect. Change it to0.0.0.0to allow connections from all devices on the network.
- If it's set to
- Make sure the MySQL service in VertrigoServ is running—restart it after making config changes to apply them.
3. Fix Your Android Connection Parameters
- Don't use
127.0.0.1as the database IP—that points to your Android device itself, not your laptop. Instead, use your laptop's local network IP (like192.168.1.102). Find this by runningipconfig(Windows) orifconfig(Mac/Linux) on your laptop. - Confirm the port number matches Vertrigo's MySQL port (default is
3306—if you changed it, update your code accordingly). - Double-check that the database name in your code is identical to what you created in MySQL (case-sensitive depending on your setup!).
4. Rule Out Network & Firewall Issues
- Ensure your Android device and laptop are on the same Wi-Fi network. Test connectivity by pinging your laptop's IP from your device (you can use a ping app or terminal if you have developer tools set up).
- Open your laptop's firewall to allow incoming connections on port
3306. For Windows, go to Firewall > Advanced Settings > Inbound Rules, then add a new rule allowing TCP traffic on port 3306 for your local network.
5. Code-Specific Fixes
- Never run database/network operations on the main thread—Android will throw a
NetworkOnMainThreadExceptionwhich can cause crashes (even if your logs say "Invalid authorization"). Move your database code to anAsyncTask, Kotlin Coroutine, or background thread. - Make sure you've added the internet permission to your
AndroidManifest.xml:<uses-permission android:name="android.permission.INTERNET" /> - Try adjusting your JDBC connection string to include these flags, which often resolve authorization/SSL hiccups in local setups:
String dbUrl = "jdbc:mysql://192.168.1.102:3306/your_db_name?useSSL=false&allowPublicKeyRetrieval=true";
Start with checking credentials and user permissions—those are the most common culprits here. Let me know if you hit any snags along the way!
内容的提问来源于stack exchange,提问作者Filippo
相关产品推荐
相关产品推荐

