Home > Database > Mysql Tutorial > body text

Detailed explanation of how to set the MySQL host name

PHPz
Release: 2024-03-02 08:36:03
Original
761 people have browsed it

Detailed explanation of how to set the MySQL host name

Detailed explanation of how to set the MySQL host name

MySQL is a commonly used relational database management system. The function of remote connection to the database can be achieved by setting the host name. In practical applications, it is often necessary to configure the MySQL host name for remote access to the database. This article will introduce in detail how to set the MySQL host name and provide specific code examples for reference.

1. The meaning of MySQL host name

In MySQL, the host name is used to identify the host that is allowed to connect to the database server. Setting the host name can limit which hosts can remotely access the database server, thereby enhancing database security.

2. How to set the MySQL host name

  1. Modify the MySQL configuration file

First, you need to modify the MySQL configuration file, usually my.cnf document. Find the bind-address line in the configuration file. It is usually set to 127.0.0.1 by default, which means it can only be accessed locally. We need to modify this to a hostname or IP address that allows remote access.

The sample code is as follows:

sudo nano /etc/mysql/my.cnf
Copy after login

Find the bind-address line and modify it to the host name or IP address that allows remote access, as shown below:

bind-address = 0.0.0.0
Copy after login

Save the file and Restart the MySQL service to make the configuration file take effect:

sudo service mysql restart
Copy after login
  1. Set MySQL user permissions

Secondly, you need to set MySQL user permissions to ensure that remote access users have the correct permissions. Including the permission to connect to the database and the permission to operate the database.

The sample code is as follows:

GRANT ALL PRIVILEGES ON *.* TO 'username'@'hostname' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Copy after login

Where, 'username' is the database username, 'hostname' is the host name that allows access to the database, and 'password' is the database password.

3. Test the remote connection

After completing the above steps, you can test whether the remote connection to the MySQL database is successful. You can use MySQL client tools, such as MySQL Workbench or Navicat, to enter the correct host name, user name, and password for connection testing.

If the connection is successful, it means that the MySQL host name is set successfully. If the connection fails, you need to check whether the configuration file and user permissions are set correctly.

Summary:

Through the introduction of this article, readers can learn how to set the MySQL host name to achieve the function of remote connection to the database. In practical applications, setting the host name appropriately can improve database security and reduce risks.

We hope that the content of this article will be helpful to readers, and we also hope that readers can apply it flexibly according to specific situations to ensure that the database connection is safe and reliable.

The above is the detailed content of Detailed explanation of how to set the MySQL host name. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!