In the process of building an LNMP environment, installing the database service is an essential step. MySQL is one of the most popular open source databases and is widely used. This article will introduce how to install MySQL on Ubuntu Server.
First, we need to add the MySQL official source. Enter the following command in the terminal:
sudo apt-get install software-properties-common sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys A4A9406876FCBD3C456770C88C718D3B5072E1F5 sudo add-apt-repository 'deb [arch=amd64] http://mirror.nodesdirect.com/mariadb/repo/10.5/ubuntu focal main'
The above command will first install software-properties-common
, then add the signing key of the official source, and finally add the MySQL source.
Next, we can install the MySQL server directly through apt-get
:
sudo apt-get update sudo apt-get install mysql-server
In the installation During the process, you will be prompted to set the password for the MySQL root account. Enter your password twice to complete the setup.
After the installation is complete, we need to perform some necessary configurations.
3.1 Modify bind-address
In order to be able to connect to the MySQL server through the network, we need to modify the bind-address of MySQL to the local IP address. Open the MySQL configuration file using the following command:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Add the following line in the [mysqld] section of the file:
bind-address = 本机ip地址
Save and close the file.
3.2 Configure remote access
If you want to access the MySQL server remotely, you need to create a new MySQL user on the server and grant it remote access permissions.
First, log in to MySQL as root:
sudo mysql -u root -p
Then, create a new user:
CREATE USER '用户名'@'%' IDENTIFIED BY '密码';
Among them, username
and password
are the username and password you set respectively.
Next, grant the user all permissions on all databases:
GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%';
Finally, refresh the permission table so that the new user can take effect:
FLUSH PRIVILEGES;
After completing the configuration, we can start the MySQL server:
sudo service mysql start
Test whether the server can connect normally:
mysql -u 用户名 -p
After entering the password, if it can succeed Connecting to the database means that the MySQL server is running normally.
Summary
Through the above steps, we have successfully installed the MySQL server on Ubuntu Server and made the necessary configurations.
In actual production environments, in order to ensure the security of the database, we usually perform more detailed configuration and management. However, the steps described in this article are enough to get you started using MySQL and accessing the database over the network.
The above is the detailed content of lnmp install mysql. For more information, please follow other related articles on the PHP Chinese website!