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 -p1234562 执行如下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.properties2 按i键进入编辑模式,找到参数spring.datasource.url,并将参数值中的端口号批改为8527。spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:8527/db_example3 批改实现后的文件内容如下所示。按下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 -p1234562 执行如下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。

在胜利页面,单击登录。

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

原文链接

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