Local Tencent cloud server remote connection of MySQL Ubuntu

1. Install MySQL

1.1, the MySQL installation (if installed skip to step 2)

sudo apt-get install mysql-server

1.2, the installation is complete landing mysql

mysql -u root -p

1.3, check the version after landing

select version(); 
 
image

1.4, this everything is normal.

2. Configure MySQL

2.1, with a landing Navicat MySQL.

(Tencent cloud Ubuntu 16.04 for example)

2.2, modify /etc/mysql/mysql.conf.d/mysqld.cnf

vim /etc/mysql/mysql.conf.d/mysqld.cnf

2.3, the bind-address = 127.0.0.1 change bind-address = 0.0.0.0

2.4, save and exit

2.5, MySQL landing

 //先输入密码登陆
mysql -root -p
//然后选择数据库
mysql>use mysql; //选择root的账户host改为%,上面2.3中已改地址,这一步不确定是否必要 mysql> update user set host='%' where user='root'; //授权 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '填写root的密码' WITH GRANT OPTION; //更新权限 FLUSH PRIVILEGES; //查询数据库用户 mysql>SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; //退出mysql重启mysql /etc/init.d/mysql restart 
 
image

2.6, if you want to add a new user, not root

//创建 test123用户,设置密码为 123456
CREATE USER test123 IDENTIFIED BY '123456'; //授权 GRANT ALL PRIVILEGES ON *.* TO 'test123'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; //更新权限 FLUSH PRIVILEGES; //退出mysql重启mysql /etc/init.d/mysql restart 

2.7, Tencent cloud open connection permission, I was here all the ports open by default, because of the convenience and nothing important things it does not matter, it is recommended only open 22,3306 port.

 
image

3.Navicat remote connections

 
image

3.1, the remote connection mysql, try sql statement insert with auto-increment primary key attribute, when skip the primary key is inserted, although successfully inserted data, but will prompt: [Err] 1055 - Expression # 1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_s
solution:

set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

4. Create a Java program to access the database or JavaWeb

4.1, mysql download jar package, click here to jump Baidu cloud download

4.2, attach Java connection code (IP account password change on the line)

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class connectDB { public static void main(String[] args) { //声明Connection对象 Connection con; //驱动程序名 String driver = "com.mysql.jdbc.Driver"; //URL指向要访问的数据库名mydata String url = "jdbc:mysql://填写你的腾讯云IP:3306/mysql"; //MySQL配置时的用户名 String user = "root"; //MySQL配置时的密码 String password = "填写你的密码"; //遍历查询结果集 try { //加载驱动程序 Class.forName(driver); //1.getConnection()方法,连接MySQL数据库!! con = DriverManager.getConnection(url,user,password); if(!con.isClosed()) System.out.println("Succeeded connecting to the Database!"); //2.创建statement类对象,用来执行SQL语句!! Statement statement = con.createStatement(); //要执行的SQL语句 String sql = "select * from user"; //3.ResultSet类,用来存放获取的结果集!! ResultSet rs = statement.executeQuery(sql); System.out.println("-----------------"); System.out.println("执行结果如下所示:"); System.out.println("-----------------"); String job = null; String id = null; while(rs.next()){ //获取stuname这列数据 job = rs.getString("user"); //输出结果 System.out.println(job); } rs.close(); con.close(); } catch(ClassNotFoundException e) { //数据库驱动类异常处理 System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch(SQLException e) { //数据库连接失败异常处理 e.printStackTrace(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ System.out.println("数据库数据成功获取!!"); } } } 

Run shot

 
image

4.3, attach JavaWeb connection code, as written in the servlet (IP account password change on the line)

<%

Run shot

 
image


Author: Superbsco
link: https://www.jianshu.com/p/e723f3d68dc7
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/macliu/p/12220030.html