关于数据库:实践教程之如何快速使用-PolarDBX

3次阅读

共计 5786 个字符,预计需要花费 15 分钟才能阅读完成。

PolarDB-X 为了不便用户体验,提供了收费的试验环境,您能够在试验环境里体验 PolarDB-X 的装置部署和各种内核个性。除了收费的试验,PolarDB-X 也提供收费的视频课程,手把手教你玩转 PolarDB-X 分布式数据库。

本期试验能够让您疾速体验 PolarDB-X,让你像应用单机 MySQL 一样应用分布式数据库,直观感触 PolarDB- X 的 MySQL 兼容性。

本期收费试验地址

本期教学视频地址

1. 前置筹备

假如曾经依据前一讲内容实现了 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 Model1

执行如下命令,创立一个 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.java2 按 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=username@example.com

返回后果如下,示意您胜利减少一条记录。

执行如下命令,查问记录。

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

在您的本机浏览器中,关上新页签,拜访 https://developer.aliyun.com/article/1114989;ECS 的弹性 IP>:9090。

在初始化页面,抉择简体中文,单击持续。

在筹备页面,单击当初就开始。

在数据库配置页面,参考阐明配置数据库信息,单击提交。

参数阐明:

数据库名:默认为 wordpress。
用户名:输出 polardbx_root。
明码:输出 123456。
数据库主机:输出 <ECS 的弹性 IP>:8527。您须要将 <ECS 的弹性 IP> 替换为云产品资源列表中的 ECS 的弹性 IP。
表前缀:默认为 wp_。

在数据库配置实现页面,单击运行安装程序。

在信息配置页面,参考阐明配置相干信息,单击装置 WordPress。

参数阐明:

站点题目:输出站点题目,例如 myblog。
用户名:输出用户名,例如 admin。
明码:输出明码。
您的电子邮箱地址:输出邮箱地址。倡议应用真实有效的邮箱地址,若没有,能够填写虚构邮箱地址,但将无奈接管信息,例如 username@example.com。

在胜利页面,单击登录。

在登录页面,顺次输入您的用户名和明码,单击登录。

原文链接

本文为阿里云原创内容,未经容许不得转载。

正文完
 0