1.安裝MySQL
1.1、安裝MySQL(若已安裝直接跳到步驟2)
sudo apt-get install mysql-server
複製程式碼
1.2、安裝完成後登陸mysql
mysql -u root -p
複製程式碼
1.3、登陸後檢視版本
select version();
複製程式碼
1.4、到此一切正常。
2.配置MySQL
2.1、用Navicat登陸MySQL。 (騰訊雲Ubuntu 16.04為例)
2.2、修改/etc/mysql/mysql.conf.d/mysqld.cnf
vim /etc/mysql/mysql.conf.d/mysqld.cnf
複製程式碼
2.3、將bind-address = 127.0.0.1更改為bind-address = 0.0.0.0
2.4、儲存退出
2.5、登陸MySQL
//先輸入密碼登陸
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
複製程式碼
2.6、若想新增新使用者,不用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、騰訊雲開放連線許可權,我這裡是預設開放所有埠,因為方便並且沒什麼重要東西就無所謂,建議只開放22,3306埠。
3.Navicat遠端連線
3.1、遠端連線mysql後,嘗試用sql語句插入帶自增主鍵屬性的表,在略過主鍵插入時,雖然成功插入資料,但是會提示: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_s
解決辦法:
解決辦法:
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.建立Java或JavaWeb專案訪問資料庫
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("資料庫資料成功獲取!!");
}
}
}
複製程式碼
執行截圖
4.3、附上JavaWeb連線程式碼,寫在servlet一樣(改IP賬戶密碼就行)
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.Statement"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
//宣告Connection物件
Connection con;
//驅動程式名
String driver = "com.mysql.jdbc.Driver";
//URL指向要訪問的資料庫名mydata
String url = "jdbc:mysql://填寫你騰訊雲的IP/mysql";
//MySQL配置時的使用者名稱
String user = "root";
//MySQL配置時的密碼
String password = "填寫你的密碼";
//遍歷查詢結果集
try {
//載入驅動程式
Class.forName(driver);
//1.getConnection()方法,連線MySQL資料庫!!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
out.print("Succeeded connecting to the Database!<br>");
//2.建立statement類物件,用來執行SQL語句!!
Statement statement = con.createStatement();
//要執行的SQL語句
String sql = "select * from user";
//3.ResultSet類,用來存放獲取的結果集!!
ResultSet rs = statement.executeQuery(sql);
out.print("-----------------<br>");
out.print("執行結果如下所示:<br>");
out.print("-----------------<br>");
String job = null;
String id = null;
while(rs.next()){
//獲取stuname這列資料
job = rs.getString("user");
//輸出結果
out.print(job+"<br>");
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
//資料庫驅動類異常處理
out.print("Sorry,can`t find the Driver!<br>");
e.printStackTrace();
} catch(SQLException e) {
//資料庫連線失敗異常處理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
out.print("資料庫資料成功獲取!!");
}
%>
</body>
</html>
複製程式碼
執行截圖