简体   繁体   中英

Connecting to remote mysql database from Windows

I'm trying to establish a connection between my windows 10 client and the ubuntu 18.04 running MySQL server.

I have created a testing java class to insert some data into the database

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

public class mysqlconnection {
    public static void main(String[] args) {
        try{
            Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.1.25:3306/cotxes","user1","password");
            Statement stmt = conn.createStatement();

            for(int i = 0; i < 10; i++){
                String insert = "INSERT INTO cotxe (nom, descripcio) VALUES ('VW Golf', 'Best car in the world')";
                stmt.executeUpdate(insert);
            }
            stmt.close();
            conn.close();
        }catch(SQLException ex){
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }catch(Exception ex){
            System.out.println("Exception: " + ex.getMessage());
        }
    }
}

Also I added the Jar's files into the project and while in my ubuntu machine works because I've replaced the ip for 'localhost' in my w10 machine doesn't and I get this error:

SQLException: null,  message from server: "Host 'msi.home' is not allowed to connect to this MySQL server"
SQLState: HY000
VendorError: 1130

This is the code in ubuntu:

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

public class mysqlconnection {
    public static void main(String[] args) {
        try{
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cotxes","user1","password");
            Statement stmt = conn.createStatement();

            for(int i = 0; i < 10; i++){
                String insert = "INSERT INTO cotxe (nom, descripcio) VALUES ('VW Golf', 'Best car in the world')";
                stmt.executeUpdate(insert);
            }
            stmt.close();
            conn.close();
        }catch(SQLException ex){
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }catch(Exception ex){
            System.out.println("Exception: " + ex.getMessage());
        }
    }
}

The "user1" has all privileges on the system like below:

| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `user1`@`localhost` WITH GRANT OPTION                                                                                                                                               |
| GRANT APPLICATION_PASSWORD_ADMIN,AUDIT_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CLONE_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,INNODB_REDO_LOG_ARCHIVE,INNODB_REDO_LOG_ENABLE,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_APPLIER,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SHOW_ROUTINE,SYSTEM_USER,SYSTEM_VARIABLES_ADMIN,TABLE_ENCRYPTION_ADMIN,XA_RECOVER_ADMIN ON *.* TO `user1`@`localhost` WITH GRANT OPTION |

And also I have tried "ping" to check the connectivity and it works from w10 to ubuntu and the opposite.

Looks like the problem is coming from external connections and MySQLServer refuses them, is there any way to allow remote connections?

Have you tried the replies here?

Host is not allowed to connect to this MySQL server for client-server application

Seems that one with all the privileges is not the issue in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM