How to quickly use 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
次のコマンドを実行して、初期ディレクトリに移動します。
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 キーを押し、保存して終了します。
エンティティモデルを作成する
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 キーを押し、保存して終了します。
ユーザーレコードを保存するリポジトリを作成する
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 { }
3 変更後のファイルの内容は次のとおりです。Esc キーを押した後、:wq と入力して Enter キーを押し、保存して終了します。
アプリケーションへの HTTP リクエストを処理する Controller クラスを作成する
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 getAllUsers() {
// This returns a JSON or XML with the users
return userRepository. findAll();
}
}
3 変更後のファイルの内容は次のとおりです。Esc キーを押した後、:wq と入力して Enter キーを押し、保存して終了します。
アプリケーションを作成する
注意:AccessingDataMysqlApplication クラスは Spring Boot サンプルプロジェクトに既に作成されているため、この手順をスキップできます。
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
約 2 分ほどお待ちください。次の結果が返されると、正常に実行されたことを示します。
テスト
実験ページで、
v2-2f7920e5bb28bdd3cf91df84c349aa03_1440w.png
アイコンをクリックして、新しいターミナルウィンドウを作成します。
新しいターミナルウィンドウで、次のコマンドを実行してレコードを追加します。
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 を設定する
ローカルブラウザで新しいタブを開き、http://<Elastic IP of ECS>:9090 にアクセスします。
初期化ページで、簡体字中国語を選択し、[続行] をクリックします。
準備ページで、[今すぐ始める] をクリックします。
データベース設定ページで、説明に従ってデータベース情報を設定し、[送信] をクリックします。
パラメーターの説明:
データベース名:デフォルトは wordpress です。
ユーザー名:polardbx_root と入力します。
パスワード:123456 と入力します。
データベースホスト::8527 と入力します。 をクラウドプロダクトリソースリストの ECS Elastic IP に置き換える必要があります。
テーブルプレフィックス:デフォルトは wp_ です。
データベース設定完了ページで、[セットアップを実行] をクリックします。
情報設定ページで、説明に従って関連情報を設定し、[WordPress をインストール] をクリックします。
パラメーターの説明:
サイトタイトル:myblog などのサイトタイトルを入力します。
ユーザー名:admin などのユーザー名を入力します。
パスワード:パスワードを入力します。
メールアドレス:メールアドレスを入力します。実在する有効なメールアドレスの使用を推奨します。使用しない場合は仮のメールアドレスを入力できますが、username@example.com のように情報を受信できません。
成功ページで、[ログイン] をクリックします。
ログインページで、ユーザー名とパスワードを順に入力し、[ログイン] をクリックします。
この体験では、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
次のコマンドを実行して、初期ディレクトリに移動します。
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 キーを押し、保存して終了します。
エンティティモデルを作成する
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 キーを押し、保存して終了します。
ユーザーレコードを保存するリポジトリを作成する
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
3 変更後のファイルの内容は次のとおりです。Esc キーを押した後、:wq と入力して Enter キーを押し、保存して終了します。
アプリケーションへの HTTP リクエストを処理する Controller クラスを作成する
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
// This returns a JSON or XML with the users
return userRepository. findAll();
}
}
3 変更後のファイルの内容は次のとおりです。Esc キーを押した後、:wq と入力して Enter キーを押し、保存して終了します。
アプリケーションを作成する
注意:AccessingDataMysqlApplication クラスは Spring Boot サンプルプロジェクトに既に作成されているため、この手順をスキップできます。
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
約 2 分ほどお待ちください。次の結果が返されると、正常に実行されたことを示します。
テスト
実験ページで、
v2-2f7920e5bb28bdd3cf91df84c349aa03_1440w.png
アイコンをクリックして、新しいターミナルウィンドウを作成します。
新しいターミナルウィンドウで、次のコマンドを実行してレコードを追加します。
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 を設定する
ローカルブラウザで新しいタブを開き、http://<Elastic IP of ECS>:9090 にアクセスします。
初期化ページで、簡体字中国語を選択し、[続行] をクリックします。
準備ページで、[今すぐ始める] をクリックします。
データベース設定ページで、説明に従ってデータベース情報を設定し、[送信] をクリックします。
パラメーターの説明:
データベース名:デフォルトは wordpress です。
ユーザー名:polardbx_root と入力します。
パスワード:123456 と入力します。
データベースホスト:
テーブルプレフィックス:デフォルトは wp_ です。
データベース設定完了ページで、[セットアップを実行] をクリックします。
情報設定ページで、説明に従って関連情報を設定し、[WordPress をインストール] をクリックします。
パラメーターの説明:
サイトタイトル:myblog などのサイトタイトルを入力します。
ユーザー名:admin などのユーザー名を入力します。
パスワード:パスワードを入力します。
メールアドレス:メールアドレスを入力します。実在する有効なメールアドレスの使用を推奨します。使用しない場合は仮のメールアドレスを入力できますが、username@example.com のように情報を受信できません。
成功ページで、[ログイン] をクリックします。
ログインページで、ユーザー名とパスワードを順に入力し、[ログイン] をクリックします。
Related Articles
-
A detailed explanation of Hadoop core architecture HDFS
Knowledge Base Team
-
What Does IOT Mean
Knowledge Base Team
-
6 Optional Technologies for Data Storage
Knowledge Base Team
-
What Is Blockchain Technology
Knowledge Base Team
Explore More Special Offers
-
Short Message Service(SMS) & Mail Service
50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00
