4.2.5. MySQL connection

发布时间 :2025-10-25 12:31:19 UTC      

Connect using mysql binary

You can use MySQL binary to enter the mysql command prompt to connect to the MySQL database.

Example

The following is a simple example of connecting to a mysql server from the command line:

[root@host]# mysql -u root -p
Enter password:******

After a successful login, a mysql > command prompt window appears, on which you can execute any SQL statement.

After the above command is executed, the successful login output is as follows:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2854760 to server version: 5.0.9

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

In the above example, we use root users to log in to the mysql server, but you can also log in with other mysql users.

If the user permissions are sufficient, any user can SQL from the command prompt window of mysql.

You can use the exit command to exit the mysql > command prompt window, as follows:

mysql> exit
Bye

Connect to MySQL using a PHP script

PHP provides mysqli_connect() Function to connect to the database.

This function takes six parameters, returns the connection ID after a successful link to MySQL, and returns FALSE if it fails.

Grammar

mysqli_connect(host, username, password, dbname,port, socket);

参数说明:

Parameters.

Description

host

Optional. Specify the hostname or IP address.

username

Optional. Specifies the MySQL user name.

password

Optional. Specify the MySQL password.

dbname

Optional. Specify the database to use by default.

port

Optional. Specifies the port number that attempts to connect to the MySQL server.

socket

Optional. Specifies the socket or named pipe to use.

You can use PHP. mysqli_close() Function to break the link to the MySQL database.

Only one argument to this function is mysqli_connect() The MySQL connection identifier returned after the function successfully created the connection.

Grammar

bool mysqli_close ( mysqli $link )

This function closes the non-persistent connection to the MySQL server associated with the specified connection identity. If not specified link_identifier Closes the last open connection

提示: There is usually no need to use the mysqli_close() Because open non-persistent connections are automatically closed after the script is executed

Example

You can try the following example to connect to your MySQL server:

Connect MySQL

<?php$dbhost='localhost';//mysql服务器主机地址$dbuser='root';//mysql用户名$dbpass='123456';//mysql用户名密码$conn=mysqli_connect($dbhost,$dbuser,$dbpass);if(!$conn){die('Could
not
connect:'.mysqli_error());}echo'数据库连接成功!';mysqli_close($conn);?>

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.