Home > Backend Development > PHP Tutorial > PHP method to achieve high availability of database containerization

PHP method to achieve high availability of database containerization

WBOY
Release: 2023-05-18 13:12:01
Original
1179 people have browsed it

With the rapid development of cloud computing, container technology has made great progress in application deployment and resource management. The database is the core of modern software development, responsible for key business logic and data storage. In order to improve the availability of the database, containerization technology has also been introduced into database deployment.

This article will introduce how to use Docker and Kubernetes to achieve high availability of PHP database containerization.

1. Dockerized MySQL database

Docker is a popular application containerization tool that can easily package applications and their dependencies into Docker images and run them in containers . We can use Docker images to containerize the MySQL database to improve its availability.

  1. Create a Dockerfile

First, create a file named Dockerfile for building the Docker image. The Dockerfile contains all the instructions needed to build the image, including specifying the base image, adding files, and setting environment variables.

FROM mysql:5.7

ADD my.cnf /etc/mysql/my.cnf

ENV MYSQL_ROOT_PASSWORD=root
Copy after login

The FROM instruction specifies the MySQL base image we will use, the ADD instruction adds the my.cnf file to the container, and sets the environment variable MYSQL_ROOT_PASSWORD to root.

  1. Build Docker image

Use the following command to build the MySQL image:

docker build -t my-mysql .
Copy after login

-t option specifies the image name as my-mysql.

  1. Run the MySQL container

Next, use the following command to run the MySQL image in the container:

docker run -d --name my-mysql -p 3306:3306 
           -e MYSQL_ROOT_PASSWORD=root 
           my-mysql
Copy after login

The -d option specifies that the container runs in the background, --name specifies the container name as my-mysql, the -p option maps the container internal port 3306 to the host port 3306, and uses environment variables to set the password of the MySQL superuser.

Now, we have successfully containerized MySQL and can access it through localhost:3306.

2. Use Kubernetes to manage MySQL containers

Kubernetes is an open source container orchestration system used to automate the deployment, expansion and management of applications. It provides many useful features such as automatic load balancing, automatic scaling, container storage, etc.

Next, we will use Kubernetes to manage the MySQL container to improve its availability.

  1. Create a Kubernetes service object

Create a Kubernetes service object that binds all MySQL containers into the same service:

apiVersion: v1
kind: Service
metadata:
  name: my-mysql
spec:
  ports:
  - port: 3306
  selector:
    app: my-mysql
Copy after login

This will create A Kubernetes service object named my-mysql listens on port 3306 inside the container.

  1. Create a Kubernetes deployment object

Create a Kubernetes deployment object, deploy multiple MySQL containers to the same Kubernetes pod, and automatically restart when the container fails:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-mysql
  labels:
    app: my-mysql
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-mysql
  template:
    metadata:
      labels:
        app: my-mysql
    spec:
      containers:
      - name: mysql
        image: my-mysql
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "root"
Copy after login

This will create a Kubernetes deployment object named my-mysql containing 3 MySQL containers. If any container fails, Kubernetes will automatically restart it.

Now, we have successfully used Kubernetes to manage the MySQL container and improve its availability.

3. Use PHP to connect to the MySQL container

Finally, we need to use PHP to connect to the MySQL container and perform some simple database operations.

  1. Create PHP file

Create a PHP file named test.php for connecting to the MySQL container and executing SQL queries:

<?php
$host = "my-mysql.default.svc.cluster.local";
$username = "root";
$password = "root";
$dbname = "test";

$conn = mysqli_connect($host, $username, $password, $dbname);

if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
  while($row = mysqli_fetch_assoc($result)) {
    echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
  }
} else {
  echo "0 results";
}

mysqli_close($conn);
?>
Copy after login

The The file connects to my-mysql.default.svc.cluster.local, connects to the MySQL container using username and password root, and retrieves data from a table named users.

  1. Deploy PHP

Upload the test.php file to the web server and start the web server. Now we can access the MySQL container by accessing the web server. For example, if the IP address of the web server is XXX.XXX.XXX.XXX, we can query the MySQL database by accessing http://XXX.XXX.XXX.XXX/test.php.

Conclusion

Containerization technology provides many benefits for modern software development, including application portability, scalability, and maintainability. By using Docker and Kubernetes, we can easily containerize the MySQL database and improve the availability and reliability of the database. If you are developing PHP applications and need to manage MySQL databases, containerization is an essential option.

The above is the detailed content of PHP method to achieve high availability of database containerization. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template