How to quickly use PolarDB-X
In order to facilitate user experience, PolarDB-X provides a free experimental environment, where you can experience the installation and deployment of PolarDB-X and various kernel features. In addition to free experiments, PolarDB-X also provides free video courses to teach you how to use the PolarDB-X distributed database.
This experiment allows you to quickly experience PolarDB-X, allowing you to use a distributed database like a stand-alone MySQL, and intuitively feel the MySQL compatibility of PolarDB-X.
1. Preparation
Assuming that the PolarDB-X has been built and deployed according to the content of the previous lecture, the PolarDB-X database can be successfully linked.
PolarDB-X: How to Quickly Install and Deploy PolarDB-X in a Practical Tutorial
2. Install JDK
Run the following command to install JDK 1.8 using yum.
yum -y install java-1.8.0-openjdk*
Execute the following command to check whether the installation is successful
java-version
The following results are returned, indicating that you have successfully installed JDK 1.8.
3. Experience Spring Boot+PolarDB-X application development
Execute the following command to install Git.
yum -y install git
Run the following command to download the Spring Boot sample project.
git clone https://github.com/spring-guides/gs-accessing-data-mysql.git
Run the following command to enter the initial directory.
cd gs-accessing-data-mysql/initial git checkout b8408e3a1e05008811d542b706107d45160556ac
Execute the following command to view the sample project code.
ls
create database
1 Execute the following command to log in to the PolarDB-X database.
mysql -h127.0.0.1 -P8527 -upolardbx_root -p123456
2 Execute the following SQL statement to create the database db_example.
create database db_example;
3 Execute the following SQL statement to create the user springuser.
create user 'springuser'@'%' identified by 'ThePassword';
4 Execute the following SQL statement to authorize the user springuser.
grant all on db_example.* to 'springuser'@'%';
5 Enter exit to exit the database.
Configure the application.properties file to connect the database to the Spring Boot sample project.
1 Execute the following command to open the application.properties configuration file.
vim src/main/resources/application.properties
2 Press i to enter the edit mode, find the parameter spring.datasource.url, and change the port number in the parameter value to 8527.
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:8527/db_example
3 The content of the file after modification is as follows. After pressing the Esc key, enter :wq and press the Enter key to save and exit.
Create Entity Model
1 Execute the following command to create a User class.
vim src/main/java/com/example/accessingdatamysql/User.java
2 Copy and paste the following code into the User class.
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 The content of the file after modification is as follows. After pressing the Esc key, enter :wq and press the Enter key to save and exit.
Create a Repository to save user records
1 Execute the following command to create a UserRepository class.
vim src/main/java/com/example/accessingdatamysql/UserRepository.java
2 Copy and paste the following code into the UserRepository class.
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 The content of the file after modification is as follows. After pressing the Esc key, enter :wq and press the Enter key to save and exit.
Create a Controller class that handles HTTP requests to the application
1 Execute the following command to create a MainController class.
vim src/main/java/com/example/accessingdatamysql/MainController.java
2 Copy and paste the following code into the MainController class.
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 The content of the file after modification is as follows. After pressing the Esc key, enter :wq and press the Enter key to save and exit.
Create an Application
Note: The AccessingDataMysqlApplication class has been created for you in the Spring Boot sample project, and you can skip this step.
1 Execute the following command to create an AccessingDataMysqlApplication class.
vim src/main/java/com/example/accessingdatamysql/AccessingDataMysqlApplication.java
2 Press the i key to enter the edit mode, copy and paste the following code into the User class.
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 The content of the file after modification is as follows. After pressing the Esc key, enter :wq and press the Enter key to save and exit.
Run the Spring Boot sample project
Execute the following command to run the Spring Boot sample project.
./gradlew bootRun
Please wait patiently for about two minutes, the returned results are as follows, which means you have successfully run.
test
On the experiment page, click the
v2-2f7920e5bb28bdd3cf91df84c349aa03_1440w.png
icon to create a new terminal window.
In a new terminal window, execute the following command to add a record.
curl localhost:8080/demo/add -d name=First -d email=username@example.com
The returned result is as follows, indicating that you have successfully added a record.
Execute the following command to query records.
curl 'localhost:8080/demo/all'
The returned results are as follows, and you can query the newly added record information.
Run the following command to log in to the PolarDB-X database.
mysql -h127.0.0.1 -P8527 -upolardbx_root -p123456
Execute the following SQL statement to use the database.
use db_example;
Execute the following SQL statement to query the user table.
select * from user;
The following results are returned, and you can query the newly added records in the user table.
Enter exit to exit the database.
4. Experience WordPress+PolarDB-X deployment blog site
This step will guide you how to build a blog site using the Wordpress Docker image and PolarDB-X. Wordpress provides a Docker image for quick installation. For details, see the WordPress Docker Hub homepage.
Install WordPress
Execute the following command to install WordPress.
docker run --name some-wordpress -p 9090:80 -d wordpress
Create WordPress database.
1 Execute the following command to log in to the PolarDB-X database.
mysql -h127.0.0.1 -P8527 -upolardbx_root -p123456
2 Execute the following SQL statement to create the database wordpress.
create database wordpress MODE='AUTO';
3 Enter exit to exit the database.
Configure WordPress
In your local browser, open a new tab and visit http://<Elastic IP of ECS>:9090.
On the initialization page, select Simplified Chinese and click Continue.
On the Prepare page, click Get started now.
On the database configuration page, refer to the instructions to configure the database information, and click Submit.
Parameter Description:
Database name: The default is wordpress.
Username: Enter polardbx_root.
Password: Enter 123456.
Database host: Enter:8527. You need to replace with the ECS Elastic IP in the cloud product resource list.
Table prefix: defaults to wp_.
On the Database Configuration Complete page, click Run Setup.
On the information configuration page, refer to the instructions to configure related information, and click Install WordPress.
Parameter Description:
Site Title: Enter a site title, such as myblog.
Username: Enter a username, such as admin.
Password: Enter a password.
Your email address: Enter your email address. It is recommended to use a real and valid email address. If not, you can fill in a virtual email address, but you will not be able to receive information, such as username@example.com.
On the success page, click Login.
On the login page, enter your user name and password in turn, and click Login.
This experiment allows you to quickly experience PolarDB-X, allowing you to use a distributed database like a stand-alone MySQL, and intuitively feel the MySQL compatibility of PolarDB-X.
1. Preparation
Assuming that the PolarDB-X has been built and deployed according to the content of the previous lecture, the PolarDB-X database can be successfully linked.
PolarDB-X: How to Quickly Install and Deploy PolarDB-X in a Practical Tutorial
2. Install JDK
Run the following command to install JDK 1.8 using yum.
yum -y install java-1.8.0-openjdk*
Execute the following command to check whether the installation is successful
java-version
The following results are returned, indicating that you have successfully installed JDK 1.8.
3. Experience Spring Boot+PolarDB-X application development
Execute the following command to install Git.
yum -y install git
Run the following command to download the Spring Boot sample project.
git clone https://github.com/spring-guides/gs-accessing-data-mysql.git
Run the following command to enter the initial directory.
cd gs-accessing-data-mysql/initial git checkout b8408e3a1e05008811d542b706107d45160556ac
Execute the following command to view the sample project code.
ls
create database
1 Execute the following command to log in to the PolarDB-X database.
mysql -h127.0.0.1 -P8527 -upolardbx_root -p123456
2 Execute the following SQL statement to create the database db_example.
create database db_example;
3 Execute the following SQL statement to create the user springuser.
create user 'springuser'@'%' identified by 'ThePassword';
4 Execute the following SQL statement to authorize the user springuser.
grant all on db_example.* to 'springuser'@'%';
5 Enter exit to exit the database.
Configure the application.properties file to connect the database to the Spring Boot sample project.
1 Execute the following command to open the application.properties configuration file.
vim src/main/resources/application.properties
2 Press i to enter the edit mode, find the parameter spring.datasource.url, and change the port number in the parameter value to 8527.
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:8527/db_example
3 The content of the file after modification is as follows. After pressing the Esc key, enter :wq and press the Enter key to save and exit.
Create Entity Model
1 Execute the following command to create a User class.
vim src/main/java/com/example/accessingdatamysql/User.java
2 Copy and paste the following code into the User class.
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 The content of the file after modification is as follows. After pressing the Esc key, enter :wq and press the Enter key to save and exit.
Create a Repository to save user records
1 Execute the following command to create a UserRepository class.
vim src/main/java/com/example/accessingdatamysql/UserRepository.java
2 Copy and paste the following code into the UserRepository class.
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 The content of the file after modification is as follows. After pressing the Esc key, enter :wq and press the Enter key to save and exit.
Create a Controller class that handles HTTP requests to the application
1 Execute the following command to create a MainController class.
vim src/main/java/com/example/accessingdatamysql/MainController.java
2 Copy and paste the following code into the MainController class.
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 The content of the file after modification is as follows. After pressing the Esc key, enter :wq and press the Enter key to save and exit.
Create an Application
Note: The AccessingDataMysqlApplication class has been created for you in the Spring Boot sample project, and you can skip this step.
1 Execute the following command to create an AccessingDataMysqlApplication class.
vim src/main/java/com/example/accessingdatamysql/AccessingDataMysqlApplication.java
2 Press the i key to enter the edit mode, copy and paste the following code into the User class.
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 The content of the file after modification is as follows. After pressing the Esc key, enter :wq and press the Enter key to save and exit.
Run the Spring Boot sample project
Execute the following command to run the Spring Boot sample project.
./gradlew bootRun
Please wait patiently for about two minutes, the returned results are as follows, which means you have successfully run.
test
On the experiment page, click the
v2-2f7920e5bb28bdd3cf91df84c349aa03_1440w.png
icon to create a new terminal window.
In a new terminal window, execute the following command to add a record.
curl localhost:8080/demo/add -d name=First -d email=username@example.com
The returned result is as follows, indicating that you have successfully added a record.
Execute the following command to query records.
curl 'localhost:8080/demo/all'
The returned results are as follows, and you can query the newly added record information.
Run the following command to log in to the PolarDB-X database.
mysql -h127.0.0.1 -P8527 -upolardbx_root -p123456
Execute the following SQL statement to use the database.
use db_example;
Execute the following SQL statement to query the user table.
select * from user;
The following results are returned, and you can query the newly added records in the user table.
Enter exit to exit the database.
4. Experience WordPress+PolarDB-X deployment blog site
This step will guide you how to build a blog site using the Wordpress Docker image and PolarDB-X. Wordpress provides a Docker image for quick installation. For details, see the WordPress Docker Hub homepage.
Install WordPress
Execute the following command to install WordPress.
docker run --name some-wordpress -p 9090:80 -d wordpress
Create WordPress database.
1 Execute the following command to log in to the PolarDB-X database.
mysql -h127.0.0.1 -P8527 -upolardbx_root -p123456
2 Execute the following SQL statement to create the database wordpress.
create database wordpress MODE='AUTO';
3 Enter exit to exit the database.
Configure WordPress
In your local browser, open a new tab and visit http://<Elastic IP of ECS>:9090.
On the initialization page, select Simplified Chinese and click Continue.
On the Prepare page, click Get started now.
On the database configuration page, refer to the instructions to configure the database information, and click Submit.
Parameter Description:
Database name: The default is wordpress.
Username: Enter polardbx_root.
Password: Enter 123456.
Database host: Enter
Table prefix: defaults to wp_.
On the Database Configuration Complete page, click Run Setup.
On the information configuration page, refer to the instructions to configure related information, and click Install WordPress.
Parameter Description:
Site Title: Enter a site title, such as myblog.
Username: Enter a username, such as admin.
Password: Enter a password.
Your email address: Enter your email address. It is recommended to use a real and valid email address. If not, you can fill in a virtual email address, but you will not be able to receive information, such as username@example.com.
On the success page, click Login.
On the login page, enter your user name and password in turn, and click Login.
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