Solved MySQL Connection ERROR

Discussion in 'Spigot Plugin Development' started by StackableGold_, Jun 10, 2017.

  1. While I was working with MySQL and went to test my plugin this came up. I'm using XAMPP; and MySQL and Apache are turned on. Everything in the config file is set correctly. Without getting this fixed I can't test my plugin any suggustions.

    P.S Sorry if this is in the wrong section this is my first plugin I'm working on and I haven't really posted anything either.

    Resulting Error:

    [00:10:46] [Server thread/WARN]: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    [00:10:46] [Server thread/WARN]: at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    [00:10:46] [Server thread/WARN]: at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    [00:10:46] [Server thread/WARN]: at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    [00:10:46] [Server thread/WARN]: at java.lang.reflect.Constructor.newInstance(Unknown Source)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2192)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2225)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2024)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    [00:10:46] [Server thread/WARN]: at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    [00:10:46] [Server thread/WARN]: at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    [00:10:46] [Server thread/WARN]: at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    [00:10:46] [Server thread/WARN]: at java.lang.reflect.Constructor.newInstance(Unknown Source)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
    [00:10:46] [Server thread/WARN]: at java.sql.DriverManager.getConnection(Unknown Source)
    [00:10:46] [Server thread/WARN]: at java.sql.DriverManager.getConnection(Unknown Source)
    [00:10:46] [Server thread/WARN]: at me.StackableGold.AEconomy.Main.mysqlSetup(Main.java:77)
    [00:10:46] [Server thread/WARN]: at me.StackableGold.AEconomy.Main.onEnable(Main.java:30)
    [00:10:46] [Server thread/WARN]: at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264)
    [00:10:46] [Server thread/WARN]: at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337)
    [00:10:46] [Server thread/WARN]: at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402)
    [00:10:46] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:377)
    [00:10:46] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:327)
    [00:10:46] [Server thread/WARN]: at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:421)
    [00:10:46] [Server thread/WARN]: at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:382)
    [00:10:46] [Server thread/WARN]: at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:337)
    [00:10:46] [Server thread/WARN]: at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272)
    [00:10:46] [Server thread/WARN]: at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:544)
    [00:10:46] [Server thread/WARN]: at java.lang.Thread.run(Unknown Source)
    [00:10:46] [Server thread/WARN]: Caused by: java.net.UnknownHostException: null
    [00:10:46] [Server thread/WARN]: at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    [00:10:46] [Server thread/WARN]: at java.net.InetAddress$2.lookupAllHostAddr(Unknown Source)
    [00:10:46] [Server thread/WARN]: at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
    [00:10:46] [Server thread/WARN]: at java.net.InetAddress.getAllByName0(Unknown Source)
    [00:10:46] [Server thread/WARN]: at java.net.InetAddress.getAllByName(Unknown Source)
    [00:10:46] [Server thread/WARN]: at java.net.InetAddress.getAllByName(Unknown Source)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:188)
    [00:10:46] [Server thread/WARN]: at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
    [00:10:46] [Server thread/WARN]: ... 27 more
     
  2. Your code is?
     
  3. This is what is responsible for connecting to the MySQL database.
    Code (Text):

    package me.StackableGold.AEconomy;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;


    public class Main extends JavaPlugin implements Listener{
        private Connection connection;
        public String host,database,username,password, table;
        public int port;
     
        public void onEnable(){
            loadconfig();
            mysqlSetup();
         
            PluginManager pm = getServer().getPluginManager();
            pm.addPermission(playerPermission);
         
         
    this.getServer().getPluginManager().registerEvents((Listener) new MySQLSetterGetter(), this);

         
         
         
        }
     
        public void loadconfig(){
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
        public void onDisable(){
         
        }
     
        //MYSQL DataBase
        public void mysqlSetup(){

            host = this.getConfig().getString("localhost");
            port = this.getConfig().getInt("port");
            database = this.getConfig().getString("database");
            username = this.getConfig().getString("username");
            password = this.getConfig().getString("password");
            table = this.getConfig().getString("table");
         
            try
            {
             
             
                synchronized (this){
                    if(getConnection() != null && !getConnection().isClosed()){
                        return;
                 
                }
                    Class.forName("com.mysql.jdbc.Driver");
                    setConnection(DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/"
                    + this.database, this.username, this.password));
                Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[Advanced Economy] MYSQL Conected!");
                }
            }catch(SQLException e){
                e.printStackTrace();
            }catch(ClassNotFoundException e){
                e.printStackTrace();
            }
        }
     
     
     
            public Connection getConnection() {
            return connection;
        }
        public void setConnection(Connection connection) {
            this.connection = connection;
    }
    }
       
     
  4. Whats your config?
     
    • Friendly Friendly x 1
  5. this is what is the config currently....
    It connects to a local database... I verified the MySQL server is running it still gives the error...

    Config.yml v

    # This is for MySQL.
    # Using a server host? Don't know your MySQL Info? Contact them.
    host: localhost
    port: 3306
    database: balances
    username: root
    password: password
    table: player_data
    balance: 500
    CurrencyName: Coins
     
  6. Just to check: You set your database credentials to something other than password for the password, yeah?
     
  7. Don't keep a connection open indefinitely. Use a connection pool/cache or open the connection when you need it.
     
  8. It's set as password and root still. I verified it.
     
  9. Nevermind. You're getting an UnknownHostException. Please try using
    Code (Text):
    host: 127.0.0.1
    instead, as it is a valid IP. localhost should be understood easily by the interpreter, but for some reason apparently not.
     
  10. Code (Text):
    Caused by: java.net.UnknownHostException: null
    Are you on windows? What about 127.0.0.1? Does your MySQL server have an ip binding in the mysql.conf?
     
    • Agree Agree x 1
  11. Make sure that there is in fact a MySQL server running where you expect it to be (host). Sounds silly, but we might as well cover all bases.
     
  12. Tried 127.0.0.1 and verified it was in the MySQL usersettings... still nothing... Also proof MySQL is up. I'm using Windows 7.[​IMG]
     
  13. Huh. Have you tried using a debug message to the console to determine that the URL is being pieced together properly? I can't see anything wrong with it but sometimes anomalies can occur where unexpected.
     
  14. Also, check your MySQL config and see if there's an IP binding in there that's something other than 0.0.0.0, or better, 127.0.0.1.
     
  15. Right um.... Let me check that.
     
  16. Okay. So i got something diffrent. I'm assuming this message is password or username related now if not what is it.. All I know is that it says the machine isn't allowed to connect...
    Code (Text):
    [01:50:37] [Server thread/WARN]: java.sql.SQLException: null,  message from server: "Host 'micheal7desktop' is not allowed to connect to this MariaDB server"
    [01:50:37] [Server thread/WARN]:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
    [01:50:37] [Server thread/WARN]:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
    [01:50:37] [Server thread/WARN]:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
    [01:50:37] [Server thread/WARN]:     at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1040)
    [01:50:37] [Server thread/WARN]:     at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2194)
    [01:50:37] [Server thread/WARN]:     at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2225)
    [01:50:37] [Server thread/WARN]:     at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2024)
    [01:50:37] [Server thread/WARN]:     at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779)
    [01:50:37] [Server thread/WARN]:     at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    [01:50:37] [Server thread/WARN]:     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    [01:50:37] [Server thread/WARN]:     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    [01:50:37] [Server thread/WARN]:     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    [01:50:37] [Server thread/WARN]:     at java.lang.reflect.Constructor.newInstance(Unknown Source)
    [01:50:37] [Server thread/WARN]:     at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    [01:50:37] [Server thread/WARN]:     at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
    [01:50:37] [Server thread/WARN]:     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
    [01:50:37] [Server thread/WARN]:     at java.sql.DriverManager.getConnection(Unknown Source)
    [01:50:37] [Server thread/WARN]:     at java.sql.DriverManager.getConnection(Unknown Source)
    [01:50:37] [Server thread/WARN]:     at me.StackableGold.AEconomy.Main.mysqlSetup(Main.java:79)
    [01:50:37] [Server thread/WARN]:     at me.StackableGold.AEconomy.Main.onEnable(Main.java:30)
    [01:50:37] [Server thread/WARN]:     at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264)
    [01:50:37] [Server thread/WARN]:     at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337)
    [01:50:37] [Server thread/WARN]:     at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402)
    [01:50:37] [Server thread/WARN]:     at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:377)
    [01:50:37] [Server thread/WARN]:     at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:327)
    [01:50:37] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:421)
    [01:50:37] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:382)
    [01:50:37] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:337)
    [01:50:37] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272)
    [01:50:37] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:544)
    [01:50:37] [Server thread/WARN]:     at java.lang.Thread.run(Unknown Source)
     
  17. Apparently it doesn't recognize you as a local host, are you doing this remotely? Users are generally configured to be local-host only, so you need to change your permissions etc.
     
  18. I'm doing this localy... Also when I'm looking in at the MyPHP page for setting passwords for MySQL... this appears...

    "A user account allowing any user from localhost to connect is present. This will prevent other users from connecting if the host part of their account allows a connection from any (%) host."

    Anyways to fix that? I dont see any local user connections with %
     
  19. Well that's self-explanatory, you have a user which looks like %@localhost, remove it.
     
  20. I didn't interpret it that way
    Code (Text):
    This will prevent other users from connecting if the host part of their account allows a connection from any (%) host.
    This does not mean that the user is named %. It means that the credentials can be used to access the account from anywhere.
    Just wanted to clarify that.