Connect to a remote database server MySQL

Foreword

This afternoon learning about JDBC, try to think of accessing MySQL database on my remote server on the local PC. This does not try do not know, a test is an afternoon ah, encountered a variety of problems large and small, all kinds of information search, the change to change to. I can Gaosi, they must write a blog about the record, the record about some big problems.

What JDK, MySQL installation configuration will not say.

I prepared a first in the local Java code, as follows:

(This code is coming from a rookie tutorial Copy: www.runoob.com/java/java-m... just localhost into my server's IP)

import java.sql.*;

public class JDBCTest {

    // JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://远程服务器IP:3306/mcl";

    // 数据库的用户名与密码,需要根据自己的设置
    static final String USER = "用户名";
    static final String PASS = "密码";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);

            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, name, url FROM websites";
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                String url = rs.getString("url");

                // 输出数据
                System.out.print("ID: " + id);
                System.out.print(", 站点名称: " + name);
                System.out.print(", 站点 URL: " + url);
                System.out.print("\n");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    }
}
复制代码

Then, MySQL database on the server database built mcl, a new library table websites mcl, and inserted 5 records. (Of course, these are also shining rookie tutorial to do)

Next, I would naive to simply start to run native Java code, the result can imagine it, how could such a simple thing! ! ? TNND! ! !

Question coming:

Question 1:

The first encounter is ClassNotFoundException. This hard to say, is not imported JDBC connector jar package. First Download this jar package, the rookie tutorial also posted links to friends: static.runoob.com/download/my... point directly into the download just fine. After downloading, import it into the project.

Question 2:

Then there is the problem of the server, the server denied access, check the information to know, you need to configure the server's security group, Ali cloud server is so allocated:

I think this issue is resolved I will be happy to access the data, it really was too naive na ~

Question 3 is coming! !

Question 3:

MySQL by default only allow the local IP (localhost / 127.0.0.1) access, if you want to let a remote device access, you need to modify the MySQL configuration file.

In the terminal cd to the directory: /etc/mysql/mysql.conf.d, inside there so the following two files to be modified is mysqld.cnf

vim mysqld.cnf edit the file, comment out the following in the box, and then save.

Not finished, in order to allow customers to access the mysql end users have access, you can authorize users through the following ways: mysql> Grant All ON . To user_name @ "%" IDENTIFIED by "user_password"; the above command to grant user rights can any database (database) and to access the mysql table (table).

(Reference here: zhidao.baidu.com/question/28... , really grateful to the chiefs of the ~)

Well, finally you can happily access a remote database it! ! (Ignore WARN, unimportant)

After the problems have been resolved, and I want to try to connect to the database remotely via Navicat, due to the above issues are resolved, the process is relatively smooth.

Directly open Navicat, click the link, then enter the server IP, data type user name, password (password database, not the server).

Then you can happily be inserted directly in the local, and delete it!

to sum up

Originally afternoon is going to look at the code compiler theory, I thought I would try it, it should be very fast, get that done went to see the code, the results emmmm ......

But then, also I learned, configure the server database with a lot of things have more understanding, but also learned a Diudiu JDBC programming! Furthermore is, a problem must be a lot to find information ......

(Good perfunctory summary, not any one forced you to manually write the dog's head ......)

Reference material

Rookie Tutorial: www.runoob.com/java/java-m...

navicat for mysql how to connect to remote database server: zhidao.baidu.com/question/28...

Introducing jar package IDEA: blog.csdn.net/superinzagh...

Guess you like

Origin juejin.im/post/5cf3a9c66fb9a07ed36e92d5
Recommended