PolarDB-X 爲了方便用戶體驗,提供了免費的實驗環境,您可以在實驗環境裏體驗 PolarDB-X 的安裝部署和各種內核特性。除了免費的實驗,PolarDB-X 也提供免費的視頻課程,手把手教你玩轉 PolarDB-X 分佈式數據庫。
本期實驗可以讓您快速體驗PolarDB-X,讓你像使用單機MySQL一樣使用分佈式數據庫,直觀感受PolarDB-X的MySQL兼容性。
1.前置準備
假設已經根據前一講內容完成了PolarDB-X的搭建部署,可以成功鏈接上PolarDB-X數據庫。
PolarDB-X:實踐教程之如何快速安裝部署PolarDB-X
2. 安裝JDK
執行如下命令,使用yum安裝JDK 1.8。
yum -y install java-1.8.0-openjdk*
執行如下命令,查看是否安裝成功
java -version
返回結果如下,表示您已成功安裝JDK 1.8。
3.體驗 Spring Boot+PolarDB-X 應用開發
執行如下命令,安裝Git。
yum -y install git
執行如下命令,下載Spring Boot樣例工程。
git clone https://github.com/spring-guides/gs-accessing-data-mysql.git
執行如下命令,進入initial目錄。
cd gs-accessing-data-mysql/initial git checkout b8408e3a1e05008811d542b706107d45160556ac
執行如下命令,查看樣例工程代碼。
ls
創建數據庫
1 執行如下命令,登錄PolarDB-X數據庫。
mysql -h127.0.0.1 -P8527 -upolardbx_root -p123456
2 執行如下SQL語句,創建數據庫db_example。
create database db_example;
3 執行如下SQL語句,創建用戶springuser。
create user 'springuser'@'%' identified by 'ThePassword';
4 執行如下SQL語句,給用戶springuser授權。
grant all on db_example.* to 'springuser'@'%';
5 輸入exit退出數據庫。
配置application.properties文件,將數據庫連接到Spring Boot樣例工程。
1 執行如下命令,打開application.properties配置文件。
vim src/main/resources/application.properties
2 按i鍵進入編輯模式,找到參數spring.datasource.url,並將參數值中的端口號修改爲8527。
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:8527/db_example
3 修改完成後的文件內容如下所示。按下Esc鍵後,輸入:wq後按下Enter鍵保存並退出。
創建Entity Model
1 執行如下命令,創建一個User類。
vim src/main/java/com/example/accessingdatamysql/User.java
2 將如下代碼複製粘貼到User類中。
package com.example.accessingdatamysql;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity // This tells Hibernate to make a table out of this class
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email; }
public void setEmail(String email) {
this.email = email;
}
}
3 修改完成後的文件內容如下所示。按下Esc鍵後,輸入:wq後按下Enter鍵保存並退出。
創建Repository,保存用戶記錄
1 執行如下命令,創建一個UserRepository類。
vim src/main/java/com/example/accessingdatamysql/UserRepository.java
2 將如下代碼複製粘貼到UserRepository類中。
package com.example.accessingdatamysql; import org.springframework.data.repository.CrudRepository; import com.example.accessingdatamysql.User; // This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository // CRUD refers Create, Read, Update, Delete public interface UserRepository extends CrudRepository<User, Integer> { }
3 修改完成後的文件內容如下所示。按下Esc鍵後,輸入:wq後按下Enter鍵保存並退出。
創建一個Controller類,處理對應用程序的HTTP請求
1 執行如下命令,創建一個MainController類。
vim src/main/java/com/example/accessingdatamysql/MainController.java
2 將如下代碼複製粘貼到MainController類中。
package com.example.accessingdatamysql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
@Autowired
// This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
@PostMapping(path="/add")
// Map ONLY POST Requests
public @ResponseBody String addNewUser (@RequestParam String name
, @RequestParam String email) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved";
}
@GetMapping(path="/all")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
3 修改完成後的文件內容如下所示。按下Esc鍵後,輸入:wq後按下Enter鍵保存並退出。
創建一個Application
說明 :Spring Boot樣例工程中已爲您創建好AccessingDataMysqlApplication類,您可跳過此步驟。
1 執行如下命令,創建一個AccessingDataMysqlApplication類。
vim src/main/java/com/example/accessingdatamysql/AccessingDataMysqlApplication.java
2 按i鍵進入編輯模式,將如下代碼複製粘貼到User類中。
package com.example.accessingdatamysql;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AccessingDataMysqlApplication {
public static void main(String[] args) {
SpringApplication.run(AccessingDataMysqlApplication.class, args);
}
}
3 修改完成後的文件內容如下所示。按下Esc鍵後,輸入:wq後按下Enter鍵保存並退出。
運行Spring Boot樣例工程
執行如下命令,運行Spring Boot樣例工程。
./gradlew bootRun
請您耐心等待大約兩分鐘,返回結果如下,表示您成功運行。
測試
在實驗頁面,單擊右上角的
圖標,創建新的終端窗口。
在新的終端窗口中,執行如下命令,增加一條記錄。
curl localhost:8080/demo/add -d name=First -d [email protected]
返回結果如下,表示您成功增加一條記錄。
執行如下命令,查詢記錄。
curl 'localhost:8080/demo/all'
返回結果如下,您可以查詢到剛剛增加的記錄信息。
執行如下命令,登錄PolarDB-X數據庫。
mysql -h127.0.0.1 -P8527 -upolardbx_root -p123456
執行如下SQL語句,使用數據庫。
use db_example;
執行如下SQL語句,查詢user表。
select * from user;
返回如下結果,您可以在user表查詢到剛剛增加的記錄。
輸入exit退出數據庫。
4.體驗WordPress+PolarDB-X部署博客站點
本步驟將指導您如何使用Wordpress的Docker鏡像和PolarDB-X搭建一個博客站點。Wordpress提供了Docker鏡像,方便其快速安裝,詳情請參見WordPress的Docker Hub主頁。
安裝WordPress
執行如下命令,安裝WordPress。
docker run --name some-wordpress -p 9090:80 -d wordpress
創建WordPress的數據庫。
1 執行如下命令,登錄PolarDB-X數據庫。
mysql -h127.0.0.1 -P8527 -upolardbx_root -p123456
2 執行如下SQL語句,創建數據庫wordpress。
create database wordpress MODE='AUTO';
3 輸入exit退出數據庫。
配置WordPress
在您的本機瀏覽器中,打開新頁籤,訪問http://<ECS的彈性IP>:9090。
在初始化頁面,選擇簡體中文,單擊繼續。
在準備頁面,單擊現在就開始。
在數據庫配置頁面,參考說明配置數據庫信息,單擊提交。
參數說明:
數據庫名:默認爲wordpress。
用戶名:輸入polardbx_root。
密碼:輸入123456。
數據庫主機:輸入<ECS的彈性IP>:8527。您需要將<ECS的彈性IP>替換爲雲產品資源列表中的ECS的彈性IP。
表前綴:默認爲wp_。
在數據庫配置完成頁面,單擊運行安裝程序。
在信息配置頁面,參考說明配置相關信息,單擊安裝WordPress。
參數說明:
站點標題:輸入站點標題,例如myblog。
用戶名:輸入用戶名,例如admin。
密碼:輸入密碼。
您的電子郵箱地址:輸入郵箱地址。建議使用真實有效的郵箱地址,若沒有,可以填寫虛擬郵箱地址,但將無法接收信息,例如[email protected]。
在成功頁面,單擊登錄。
在登錄頁面,依次輸入您的用戶名和密碼,單擊登錄。
本文爲阿里雲原創內容,未經允許不得轉載。