mysql installation and configuration guide: detailed steps to install MySQL 8 using binary decompression package on CentOS

introduction

  MySQL is a widely used relational database management system. In this article, we will introduce how to install MySQL 8 under CentOS 7.6 by binary decompression package.

1. Preparations

1.1 Hardware requirements

  • Processor : A processor of 2 GHz or higher is recommended to ensure that it can handle the demands of database operations.
  • Memory : At least 2 GB of memory (recommended from 4G) to ensure that MySQL can run efficiently. The actual memory required depends on the size and load of the database. For larger or busy databases, more memory may be required.
  • Storage space : At least 20GB of disk space (recommended 40GB of free space), make sure there is enough disk space to store MySQL and related data. This will include storage of MySQL installation files, data files, log files, and backup files.

1.2 System Requirements

  • Operating System: Make sure your operating system is CentOS 7.6 or higher. You can check your CentOS version by running:cat /etc/centos-release

  • Internet connection : In order to download the MySQL 8 binary package and necessary dependencies, ensure that the server has a reliable Internet connection. You can test your network connection with the following command: ping www.mysql.comMake sure you can successfully connect to the external network.

  • User Privileges : Before proceeding with the installation, make sure you have sufficient privileges to install software and perform system configuration. It is recommended to install using a user with administrator privileges. You can use the sudo command to perform operations that require privileges.

  • glibc version : the system parameters needed for downloading, which can be queried through the getconf GNU_LIBC_VERSIONor ldd --versioncommand.

[root@localhost ~]# getconf GNU_LIBC_VERSION
glibc 2.17
[root@localhost ~]# ldd --version
ldd (GNU libc) 2.17
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.
[root@localhost ~]# 

  • cpu architecture : the system parameters that need to be used when downloading, can be queried through the uname -mor archcommand.
[root@localhost ~]# uname -m
x86_64
[root@localhost ~]# arch
x86_64
[root@localhost ~]# 

1.3 Installation dependencies

  Before installing the binary decompression package of MySQL 8, some dependencies need to be installed to ensure that MySQL can run normally. Here are some common dependencies:

  • libaio : MySQL needs the libaio library to handle asynchronous input and output operations
  • numactl : If your system has NUMA (Non-Uniform Memory Access) architecture, it is recommended to install the numactl library to optimize the performance of MySQL on NUMA systems
  • zlib : MySQL uses the zlib library to provide data compression and decompression functions
  • openssl : MySQL uses the openssl library for encryption and security
  • cmake : When installing MySQL, you need to use the cmake tool to build and compile

Execute the following command to update the above plug-in (about 9.2M in total , it does not take a lot of time)

yum -y install libaio numactl zlib openssl cmake 

1.4 Remove MariaDB

  MariaDB is installed by default in CentOS, which is a branch of MySQL and is mainly maintained by the open source community. CentOS 7 and above versions no longer use the MySQL database, but use the MariaDB database. Uninstall the built-in MariaDB first, and then install MySQL to avoid potential conflicts or configuration problems during the MySQL installation process.

# 搜索
rpm -qa|grep mariadb
# 移除,文件名对应上面搜索出来的名字
rpm -e --nodeps {文件名}

as shown below

[root@localhost ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
[root@localhost ~]# rpm -qa|grep mariadb
[root@localhost ~]# 

2 downloads

2.1 Login to the official website

Login to the download page
insert image description here

2.2 Download the corresponding version

Select the corresponding download option according to the glibc version and cpu architecture in the previous system parameters

If you have the same version as me, you can use my connection to download directly
https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.33-linux-glibc2.17-x86_64-minimal.tar

insert image description here
Skip login, choose to download directly
insert image description here

3 installation

3.1 Upload files

Upload the downloaded file to our service, here I download it to /home, and print the file information

[root@localhost home]# ll
total 425872
-rw-r--r--. 1 root root 425871360 May 28 13:13 mysql-8.0.33-linux-glibc2.17-x86_64-minimal.tar
[root@localhost home]# 

3.2 Unzip the file

  1. Unzip the compressed package and execute it tar xvf mysql-8.0.33-linux-glibc2.17-x86_64-minimal.tar. After execution, the following three files will be extracted.

mysql-test-8.0.33-linux-glibc2.17-x86_64-minimal.tar.xz: test framework, used for unit, regression and conformance testing of mysql services, and provides the ability to run unit tests and create new unit tests The tool
mysql-8.0.33-linux-glibc2.17-x86_64-minimal.tar.xz: the necessary file to install mysql
mysql-router-8.0.33-linux-glibc2.17-x86_64-minimal.tar.xz: official A lightweight middleware provided, the main function is to provide transparent routing between the application and the MySQL server, which is the building block of the high availability (HA) solution

  1. Unzip the main file, execute tar xvf mysql-8.0.33-linux-glibc2.17-x86_64-minimal.tar.xz, and unzip the folder mysql-8.0.33-linux-glibc2.17-x86_64-minimal
  2. Move the directory to /usr/local/mysql (you can modify it according to your preferences), executemv mysql-8.0.33-linux-glibc2.17-x86_64-minimal /usr/local/mysql

3.2 Installation command

3.2.1 Execute the installation command

Enter the /usr/local/mysql directory, use the root authority to execute the following installation commands line by line (commands are explained in 3.2.2)

1. groupadd mysql
2. useradd -r -g mysql -s /bin/false mysql
3. mkdir mysql-files
4. chown mysql:mysql mysql-files
5. chmod 750 mysql-files
6. bin/mysqld --initialize --user=mysql
7. bin/mysql_ssl_rsa_setup
8. bin/mysqld_safe --user=mysql &

The execution process is as follows. You can see that the program generates a random password for us when executing step 6 initialization. Note this will be used for login later.

[root@localhost home]# cd /usr/local/mysql/
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]#  useradd -r -g mysql -s /bin/false mysql
[root@localhost mysql]# mkdir mysql-files
[root@localhost mysql]# chown mysql:mysql mysql-files
[root@localhost mysql]#  chmod 750 mysql-files
[root@localhost mysql]# bin/mysqld --initialize --user=mysql
2023-05-28T07:02:36.342000Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.33) initializing of server in progress as process 7853
2023-05-28T07:02:36.368565Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-05-28T07:02:38.108072Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-05-28T07:02:39.268898Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Zy-;br.I_2u8
[root@localhost mysql]# bin/mysql_ssl_rsa_setup
[root@localhost mysql]# bin/mysqld_safe --user=mysql &
[1] 7894
[root@localhost mysql]# Logging to '/usr/local/mysql/data/localhost.err'.
2023-05-28T07:02:52.662359Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

Installation command source official website installation guide

3.2.2 Explanation of installation commands

The explanations of the above installation commands are:

  1. Create a user group named "mysql"
  2. Create a system user named "mysql", add it to the "mysql" user group, and set its login shell to /bin/false to limit the user's login permissions
  3. Create a directory named mysql-files for storing MySQL data files
  4. Set the ownership (owner and group) of the mysql-files directory to the "mysql" user and group
  5. Set the permissions of the mysql-files directory to 750 to ensure that only members of the "mysql" user group can read, write, and execute the directory
  6. Initialize the MySQL database and set the user to "mysql". This command will create system tables, assign passwords and generate other necessary data files
  7. Generate SSL keys and certificates for MySQL for secure communication
  8. Start the MySQL server as the "mysql" user. mysqld_safe is a script for starting and monitoring a MySQL server

3.2.3 Inspection service

After installing and starting through 3.2.1, you can check whether the process of the mysql service is started, and create a new operation window to executeps -ef|grep mysql

[root@localhost ~]# ps -ef|grep mysql
root       7894   7269  0 15:02 pts/0    00:00:00 /bin/sh bin/mysqld_safe --user=mysql
mysql      7964   7894  0 15:02 pts/0    00:00:08 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.err --pid-file=localhost.pid
root       8035   7783  0 15:17 pts/1    00:00:00 bin/mysql -u root -p
root       8061   8045  0 15:21 pts/2    00:00:00 grep --color=auto mysql
[root@localhost ~]# 

3.2.4 Login and change password

After executing the above startup command, the operation window has been occupied. We need to create a new operation window and enter the /usr/local/mysql directory to execute the startup command. bin/mysql -u root -pIt is a command to log in to the server. At this time, we will be prompted to enter a password. This password is the password in the sixth step above, and the execution process is as follows

[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# bin/mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.33

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

After entering the client above, modify the password by modifying the field of the user table through the operation, and change the user password to 123456

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

IDENTIFIED WITH mysql_native_password : This is the new authentication method to be set for the user. We change the user's authentication method to mysql_native_password. This is a commonly used authentication plugin in MySQL.

flush privileges : reload and refresh the user permissions and permission table, so that the modified permissions take effect immediately (but it does not affect the established connection, the client will take effect only when a new connection is established)

3.2.5 Allow remote login

Continue to execute in the mysql client, by updating the user field to modify the remote user root account to log in

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set user.Host='%'where user.User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

3.2.6 Use Navicat client to connect

The ip of the machine where the service I installed is: 192.168.3.51, the default port is 3306, enter the account root, password 123456, click Test Connection

insert image description here

4. Start with configuration file

MySQL programs can read startup options from option files (sometimes called configuration files). Configuration files provide a convenient way to specify commonly used options so that they don't need to be entered on the command line every time a program is run.

4.1 Closing the service

First close the mysql service that has been started

[root@localhost ~]# ps -ef|grep mysql
root       7894   7269  0 15:02 pts/0    00:00:00 /bin/sh bin/mysqld_safe --user=mysql
mysql      7964   7894  0 15:02 pts/0    00:00:08 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.err --pid-file=localhost.pid
root       8035   7783  0 15:17 pts/1    00:00:00 bin/mysql -u root -p
root       8061   8045  0 15:21 pts/2    00:00:00 grep --color=auto mysql
[root@localhost ~]# kill -9 7894
[root@localhost ~]# kill -9 7964
[root@localhost ~]# ps -ef|grep mysql
root       8098   8045  0 15:46 pts/2    00:00:00 grep --color=auto mysql
[root@localhost ~]# 

4.2 Create a configuration file

The previous installation and startup all use the default configuration, which can be started by creating the my.cnf file in the /etc directory,

[root@localhost ~]# touch /etc/my.cnf
[root@localhost ~]# vi /etc/my.cnf

Fill in the configuration content below

[client]
port = 3306
#没有就创建,默认就在这里
socket = /tmp/mysql.sock

[mysqld]
###############################基础设置#####################################
#Mysql服务的唯一编号 每个mysql服务Id需唯一
server-id = 1
#服务端口号 默认3306
port = 3306
#mysql安装根目录
basedir = /usr/local/mysql
#mysql数据文件所在位置 没有改目录则创建
datadir = /usr/local/mysql/data
#临时目录 比如load data infile会用到
tmpdir = /tmp
#设置socke文件所在目录
socket = /tmp/mysql.sock
#数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character-set-server = utf8mb4
#数据库字符集对应一些排序等规则,注意要和character-set-server对应
collation-server = utf8mb4_general_ci
#设置client连接mysql时的字符集,防止乱码
init_connect='SET NAMES utf8mb4'
default-storage-engine=INNODB
###############################日志设置#####################################
#数据库错误日志文件
log_error = error.log

Execute the start command after saving, bin/mysqld_safe --user=mysql &
check the process, execute ps -ef|grep mysql
if successful, display

[root@localhost mysql]# bin/mysqld_safe --user=mysql &
[1] 8595
[root@localhost mysql]# 2023-05-28T08:07:54.842424Z mysqld_safe Logging to '/usr/local/mysql/data/error.log'.
2023-05-28T08:07:54.873484Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

[root@localhost mysql]# 

[root@localhost mysql]# ps -ef|grep mysql
root       8595   7269  0 16:07 pts/0    00:00:00 /bin/sh bin/mysqld_safe --user=mysql
mysql      8835   8595 13 16:07 pts/0    00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=error.log --pid-file=localhost.pid --socket=/tmp/mysql.sock --port=3306
root       8879   7783  0 16:08 pts/1    00:00:00 grep --color=auto mysql
[root@localhost mysql]# 

5. Configure startup

5.1 Copy startup script

The startup script is the mysql.server file in the support-files/ directory, copy it to the /etc/init.d/ directory (after copying, the name can be modified or continue to use mysql.server)

[root@localhost mysql]# cd support-files/
[root@localhost support-files]# pwd
/usr/local/mysql/support-files
[root@localhost support-files]# cp mysql.server /etc/init.d/mysql.server
[root@localhost support-files]# 

5.2 Modify the startup script

Modify the basedir and datadir in the startup script mysql.server to be consistent with the above my.cnf configuration file. For example, mine is

basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

Save after modification

5.3 Add startup

To configure booting, execute the following command

chkconfig --add /etc/init.d/mysql.server
chkconfig mysql.server on

Execute the check command chkconfig --list mysql.server, if you see that the status of 2, 3, 4, and 5 is on or on, it means success, and if it is not successful, execute the chkconfig --level 2345 mysql.server on
execution process

[root@localhost support-files]# chkconfig --add /etc/init.d/mysql.server
[root@localhost support-files]# chkconfig mysql.server on
[root@localhost support-files]# chkconfig --list mysql.server

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysql.server   	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@localhost support-files]# 

There are instructions in the mysql.server file: Default-Start: 2 3 4 5

5.4 Common management commands

  1. Start the MySQL service
sudo service mysql.server start
或
sudo systemctl start mysql.server
  1. Stop the MySQL service
sudo service mysql.server stop
或
sudo systemctl stop mysql.server
  1. Restart the MySQL service
sudo service mysql.server restart
或
sudo systemctl restart mysql.server
  1. View MySQL service status
sudo service mysql.server status
或
sudo systemctl status mysql.server

Summarize

  This article introduces in detail the process of installing MySQL 8 using the binary decompression package on the CentOS 7.6 operating system. We begin with an overview of preparations, including hardware requirements, system requirements, and installation dependencies. Then, we detailed the process of downloading MySQL 8, from logging in to the official website to selecting the appropriate version. In the installation section, we demonstrated the steps of uploading, decompressing, and installing MySQL 8 step by step, including executing installation commands, checking service status, logging in and changing passwords, allowing remote login, and using Navicat client to connect to MySQL. In addition, this article also provides the configuration file to start the MySQL service and the method of configuring the startup, and introduces some commonly used management commands. Through the guidance of this article, I hope readers can successfully install and configure MySQL 8, and start using powerful database functions.

Guess you like

Origin blog.csdn.net/dougsu/article/details/130816827