How to install and configure mysql in Ubuntu

MySQL is the world's most popular open source database. According to DB-Engines survey data, MySQL is the second most popular database, second only to Oracle database. MySQL has become the most popular open source database in the past due to its high performance, low cost, and good reliability, and is therefore widely used in small and medium-sized websites on the Internet. As MySQL continues to mature, it is gradually used in more large-scale websites and applications, such as Wikipedia, Google, and Facebook.

Below we introduce the installation and configuration of mysql under the Ubuntu system of Linux.

Install

  • First update the apt source
sudo apt update
  • Download mysql-server
sudo apt install mysql-server
  • Check the status of mysql
sudo service mysql status

The above picture shows that mysql is not currently enabled. It can be enabled through the following command.

sudo service mysql start

Check the status again:

You can see that mysql is currently running.

  • Set root password

After installing mysql for the first time, we may need to set a password for root

#进入mysql终端
sudo mysql

#设置root密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Just replace the password with the corresponding password, so that you will not be able to log in through sudo mysql next time. If you still use it, an error will be reported:

  • Login mysql
mysql -u root -p

Enter the password you just set to log in.

*If you want to go back to logging in with sudo mysql, you can use the following command:

ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;

Configuration

MySQL configuration can be modified in the configuration file. On most Linux systems, MySQL's configuration files are usually located in /etc/mysql/my.cnfor /etc/my.cnf.

  • Listening IP and port

bind-addressOption is used to specify the IP address that the MySQL server listens on. By default, it is set to 127.0.0.1, meaning that only the local loopback address is listened. If you want the MySQL server to be accessible to the external network, you can set this to the server's IP address or 0.0.0.0. portThe option is used to specify the port number that the MySQL server listens on. The default is 3306.

You can view the currently listening IP and port in the mysql command interface.

If you want to modify it, you can edit the mysql.cnf file under /etc/mysql:

#例如修改端口号为3307
[mysqld]
port=3307

Then restart mysql

service mysql restart

View current port number

You can see that it was successfully modified to 3307.

Here is a brief introduction to some other configurations. The specific modification operations are basically the same as the above-mentioned operation of modifying the port number.

  • data directory

datadirOption is used to specify the directory path where the MySQL server stores data. By default it is set to/var/lib/mysql

  • Log file settings

MySQL provides multiple types of logging, such as error logs, query logs, and slow query logs. These logs can be enabled and configured using log_errorthe , general_logand options.slow_query_log

  • Memory and cache settings

innodb_buffer_pool_sizeOption used to set the buffer pool size used by the InnoDB storage engine. It affects the performance of InnoDB tables. key_buffer_sizeOption to set the key buffer size used by the MyISAM storage engine.

  • Security Settings

skip-networkingoption is used to disable the MySQL server from connecting over the network. skip-name-resolveOption to disable DNS resolution to increase connection speed and security.

Guess you like

Origin blog.csdn.net/m0_56572447/article/details/131148198