PHP is a widely used dynamic web development language. In order for website data to be permanently saved and managed, a database must be used. This article will describe the steps for installing a PHP website database, hoping to be helpful to developers who need to build a PHP website.
1. Select the database type
Before installing the database, you must first select the database type. Currently commonly used relational database management systems include MySQL, Oracle, and SQL Server, while non-relational database management systems such as MongoDB and Redis are also developing rapidly. Most PHP websites will choose MySQL as their database management system because MySQL has the advantages of open source, stability, and ease of use.
2. Install the database
1. Download the MySQL database installation program
MySQL official website provides a MySQL installation program that can be used directly on Windows or Linux systems. Select the corresponding operating system on the official website download page and download the MySQL installation program.
2. Run the MySQL installation program
Double-click the MySql installation program and follow the prompts to install step by step. During the entire installation process, there will be some options that need to be set, such as the MySQL installation folder path, login password, database configuration port, etc. Remember when choosing a username and password, choose one that is secure. If you are using a production server, the password must contain uppercase letters, lowercase letters, numbers, and special symbols.
3. Start the MySQL service
After the MySQL installation is completed, you first need to start the MySQL service. On Windows systems, the MySQL service can be started in the Services application. On a Linux system, use the Shell command line and enter the "service mysqld start" command to start the MySQL service.
3. Create database and user
After the MySQL installation is completed and the service is started, you need to create the database and user. In order to ensure data security, it is recommended to use an independent user to manage the database instead of using super administrator (root) privileges to log in to MySQL.
1. Log in to MySQL
Use the MySQL account and password that have been set to log in to MySQL. You can use command line tools or visual tools such as Navicat to log in to MySQL.
2.Create user
CREATE USER 'Username'@'Host Address' IDENTIFIED BY 'Password';
For example, create a user named testuser with a password For testpassword, you can use the following command:
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'testpassword';
3. Create a database
CREATE DATABASE database name ;
For example, to create a database named phpdatabase, you can use the following command:
CREATE DATABASE phpdatabase;
4. Authorize the user
GRANT permissions ON database name. table name TO 'username'@'host address';
For example, to give the testuser user all permissions to the phpdatabase database, you can use the following command:
GRANT ALL PRIVILEGES ON phpdatabase.* TO 'testuser'@'localhost';
4. PHP configuration file modification
PHP's configuration file is php.ini, which needs to be modified to make the PHP program use the existing database. Open the php.ini file and find the following three configurations:
;extension=php_mysql.dll
;extension=php_mysqli.dll
;extension=php_pdo_mysql.dll
will be deleted semicolon in front, save and restart the server so that the PHP program can use the MySQL database.
5. Connecting to the database
PHP needs to use the mysqli function library to connect to the MySQL database. The following is the most basic example of connecting to the database:
$servername = "localhost"; //Database service name
$username = "testuser"; //Database username
$password = "testpassword"; //Database password
$dbname = "phpdatabase"; / /Database name
//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection status
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "Connection successful";
?>
The installation steps are introduced here. Through the above steps, You can build a PHP website on your own server and use the MySQL database for data management. Of course, this is just the tip of the iceberg. If you want to use it in a production environment, you need to further consider security, performance optimization and other aspects.
The above is the detailed content of Let's talk about the installation steps of PHP website database. For more information, please follow other related articles on the PHP Chinese website!