[Configuration environment] Install MySQL under Linux

Table of contents

One, the environment

Two, installation steps

1. Install MySQL using the package manager

2. Configure MySQL security options

3. Set the root user to authenticate with a password (optional)

Third, expand knowledge

1. How to modify the MySQL password policy?

2. Implement the test code for connecting to the MySQL database


One , the environment

  • VMware® Workstation 16 Pro (Version: 16.1.2 build-17966106)
  • ubuntu-22.04.2-desktop-amd64

Two , installation steps

1. Install MySQL using the package manager

  • For Ubuntu/Debian systems, use the apt-get command to install:
    • sudo apt-get update
    • sudo apt-get install mysql-server
  • For CentOS/RHEL systems, use the yum command to install:
    • sudo yum update
    • sudo yum install mysql-server
  • After the installation is complete, the MySQL service will start automatically. Check the status of the MySQL service with the following command:
    • sudo systemctl status mysql
  • If the MySQL service does not start automatically, you can manually start the MySQL service:
    • For Ubuntu/Debian systems, use the following command:
      • sudo service mysql start
    • For CentOS/RHEL systems, use the following command:
      • sudo systemctl start mysql
  • You can check whether the MySQL service is running with the following command:
    • sudo systemctl is-active mysql
  • The command to stop the MySQL service is as follows:
    • sudo systemctl stop mysql

2. Configure MySQL security options

  • For MySQL 5.7 and above, run the following command for initial configuration: (this command will ask you to set the MySQL root user password and other security options)
    • sudo mysql_secure_installation
  • Then prompt whether to configure the password verification component, enter y
  • Next, it will display the strength of choosing to set the password, enter 0
  • Ask if you want to remove the anonymous user, enter y ( after selecting the password strength in the previous step, there will be a process of entering the password to be set, but skip it directly, see the next section for the reason )
  • Ask whether to disable the root user for remote login, enter y
  • Ask whether to delete the test database and access it, enter y
  • Asking whether to reload the permissions table means that the settings made so far will take effect immediately. enter y
  • Finally, All done appears, indicating that the setting is complete!
  • After the installation is complete, you can log in to the MySQL server through the following command (at this time, the authentication is performed through the auth_socket plug-in)
    • sudo mysql

3. Set the root user to authenticate with a password (optional)

  • When configuring the security options of MySQL, after choosing to set the strength of the password, the step of entering the password is directly skipped, and the content in the red box is displayed as follows
  • This is because in MySQL, by default, the root user is authenticated using the auth_socket plugin instead of using a password. The auth_socket plugin authenticates user credentials via a Unix socket file. Therefore, it is not necessary to set a password for the root user if using the auth_socket plugin for authentication.

If you wish to authenticate with a password for the root user, you can follow these steps:

  • Log in to MySQL as the root user (authentication via the auth_socket plugin at this time):
    • sudo mysql
  • After logging in, switch to the MySQL system database:
    • USE mysql;
  • Update the root user's authentication method to use the mysql_native_password plugin: (replace your_password with the root user's password, which must be at least 8 characters long)
    • ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
  • Refresh the permissions table for the changes to take effect:
    • FLUSH PRIVILEGES;
  • After completing these steps, the root user will be able to authenticate via password instead of via the auth_socket plugin.
  • Finally, use the following command and enter the MySQL root user password set earlier to log in to the MySQL server:
    • mysql -u root -p

Third , expand knowledge

1. How to modify the MySQL password policy?

  • When setting the MySQL login password, you will encounter the following error message
  • This error occurs because MySQL has a password policy enabled by default to ensure password security. According to the password policy, the password must meet certain requirements before it can be accepted, and the requirements are as follows in the red box.
  • But in MySQL, you can adjust the password policy requirements by modifying the password policy variables. Specifically, the following two related variables can be modified:
    • validate_password.policy: This variable defines the password policy requirements. Its value can be a combination of one or more of the following policies.
      • LOW: The password only requires the length to be satisfied.
      • MEDIUM: The password must contain at least numbers, letters, and special characters.
      • STRONG: Password must contain at least numbers, letters, special characters, and other characters.
    • validate_password.length: This variable defines the minimum password length requirement, the default is 8, and can be adjusted as needed.

Password policies can be adjusted in MySQL by following these steps:

  • Log in to MySQL as root:
    • sudo mysql
  • Execute the following command to modify the password policy: (set the password policy to LOW, and set the minimum password length to 4)
    • SET GLOBAL validate_password.policy='LOW';
    • SET GLOBAL validate_password.length=4;
  • Refresh the permissions table for the changes to take effect:
    • FLUSH PRIVILEGES;
  • Exit the MySQL command line interface:
    • QUIT;

2. Implement the test code for connecting to the MySQL database

#include <mysql/mysql.h>
#include <stdio.h>

int main() {
    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;

    char *server = "localhost";
    char *user = "username";
    char *password = "password";
    char *database = "database_name";

    conn = mysql_init(NULL);

    // 连接数据库
    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        mysql_close(conn);
        return 1;
    }

    // 执行SQL查询
    if (mysql_query(conn, "SELECT * FROM table_name")) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        return 1;
    }

    res = mysql_use_result(conn);

    // 处理查询结果
    while ((row = mysql_fetch_row(res)) != NULL) {
        for(int i = 0; i < mysql_num_fields(res); i++) {
            printf("%s ", row[i] ? row[i] : "NULL");
        }
        printf("\n");
    }

    // 释放结果集和关闭连接
    mysql_free_result(res);
    mysql_close(conn);

    return 0;
}
  • Make sure the library is installed  libmysqlclient-dev . If it is not installed, you can install it with the following command:
    • sudo apt-get install libmysqlclient-dev
  • In the compilation command, the MySQL C API library needs to be linked. It can be compiled with the following command:
    • gcc -o my_program main.c -lmysqlclient
  • If you still have problems, make sure the compiler can correctly find the path to the MySQL library files. If the MySQL library file is not in the default path, it may be necessary to use  -L the flag to specify the path of the library file.

Guess you like

Origin blog.csdn.net/weixin_43729127/article/details/132251704