更多的可以參考我的博客冰评,也在陸續(xù)更新ing
http://www.hspweb.cn/
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
//然后選擇數(shù)據(jù)庫(kù)
mysql>use mysql;
//選擇root的賬戶(hù)host改為%惯雳,上面2.3中已改地址朝巫,這一步不確定是否必要
mysql> update user set host='%' where user='root';
//授權(quán)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '填寫(xiě)root的密碼' WITH GRANT OPTION;
//更新權(quán)限
FLUSH PRIVILEGES;
//查詢(xún)數(shù)據(jù)庫(kù)用戶(hù)
mysql>SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
//退出mysql重啟mysql
/etc/init.d/mysql restart
2.6、若想添加新用戶(hù)石景,不用root
//創(chuàng)建 test123用戶(hù)劈猿,設(shè)置密碼為 123456
CREATE USER test123 IDENTIFIED BY '123456';
//授權(quán)
GRANT ALL PRIVILEGES ON *.* TO 'test123'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
//更新權(quán)限
FLUSH PRIVILEGES;
//退出mysql重啟mysql
/etc/init.d/mysql restart
2.7、騰訊云開(kāi)放連接權(quán)限潮孽,我這里是默認(rèn)開(kāi)放所有端口揪荣,因?yàn)榉奖悴⑶覜](méi)什么重要東西就無(wú)所謂,建議只開(kāi)放22往史,3306端口仗颈。
3.Navicat遠(yuǎn)程連接
3.1、遠(yuǎn)程連接mysql后椎例,嘗試用sql語(yǔ)句插入帶自增主鍵屬性的表挨决,在略過(guò)主鍵插入時(shí)请祖,雖然成功插入數(shù)據(jù),但是會(huì)提示: [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.創(chuàng)建Java或JavaWeb項(xiàng)目訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)
4.1脖祈、下載mysql的jar包,點(diǎn)擊此跳轉(zhuǎn)百度云下載
4.2肆捕、附上Java連接代碼(改IP賬戶(hù)密碼就行)
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對(duì)象
Connection con;
//驅(qū)動(dòng)程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要訪(fǎng)問(wèn)的數(shù)據(jù)庫(kù)名mydata
String url = "jdbc:mysql://填寫(xiě)你的騰訊云IP:3306/mysql";
//MySQL配置時(shí)的用戶(hù)名
String user = "root";
//MySQL配置時(shí)的密碼
String password = "填寫(xiě)你的密碼";
//遍歷查詢(xún)結(jié)果集
try {
//加載驅(qū)動(dòng)程序
Class.forName(driver);
//1.getConnection()方法,連接MySQL數(shù)據(jù)庫(kù)8歉摺慎陵!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.創(chuàng)建statement類(lèi)對(duì)象,用來(lái)執(zhí)行SQL語(yǔ)句;蛭琛荆姆!
Statement statement = con.createStatement();
//要執(zhí)行的SQL語(yǔ)句
String sql = "select * from user";
//3.ResultSet類(lèi),用來(lái)存放獲取的結(jié)果集S车省胆筒!
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("執(zhí)行結(jié)果如下所示:");
System.out.println("-----------------");
String job = null;
String id = null;
while(rs.next()){
//獲取stuname這列數(shù)據(jù)
job = rs.getString("user");
//輸出結(jié)果
System.out.println(job);
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
//數(shù)據(jù)庫(kù)驅(qū)動(dòng)類(lèi)異常處理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//數(shù)據(jù)庫(kù)連接失敗異常處理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("數(shù)據(jù)庫(kù)數(shù)據(jù)成功獲取U┩恪仆救!");
}
}
}
運(yùn)行截圖
4.3、附上JavaWeb連接代碼矫渔,寫(xiě)在servlet一樣(改IP賬戶(hù)密碼就行)
<%@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對(duì)象
Connection con;
//驅(qū)動(dòng)程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要訪(fǎng)問(wèn)的數(shù)據(jù)庫(kù)名mydata
String url = "jdbc:mysql://填寫(xiě)你騰訊云的IP/mysql";
//MySQL配置時(shí)的用戶(hù)名
String user = "root";
//MySQL配置時(shí)的密碼
String password = "填寫(xiě)你的密碼";
//遍歷查詢(xún)結(jié)果集
try {
//加載驅(qū)動(dòng)程序
Class.forName(driver);
//1.getConnection()方法彤蔽,連接MySQL數(shù)據(jù)庫(kù)!庙洼!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
out.print("Succeeded connecting to the Database!<br>");
//2.創(chuàng)建statement類(lèi)對(duì)象顿痪,用來(lái)執(zhí)行SQL語(yǔ)句!油够!
Statement statement = con.createStatement();
//要執(zhí)行的SQL語(yǔ)句
String sql = "select * from user";
//3.ResultSet類(lèi)蚁袭,用來(lái)存放獲取的結(jié)果集!石咬!
ResultSet rs = statement.executeQuery(sql);
out.print("-----------------<br>");
out.print("執(zhí)行結(jié)果如下所示:<br>");
out.print("-----------------<br>");
String job = null;
String id = null;
while(rs.next()){
//獲取stuname這列數(shù)據(jù)
job = rs.getString("user");
//輸出結(jié)果
out.print(job+"<br>");
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
//數(shù)據(jù)庫(kù)驅(qū)動(dòng)類(lèi)異常處理
out.print("Sorry,can`t find the Driver!<br>");
e.printStackTrace();
} catch(SQLException e) {
//數(shù)據(jù)庫(kù)連接失敗異常處理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
out.print("數(shù)據(jù)庫(kù)數(shù)據(jù)成功獲瓤摹!鬼悠!");
}
%>
</body>
</html>
運(yùn)行截圖