Installing MySQL 8 under linux

Posted by alefort on Sun, 23 Jan 2022 21:10:45 +0100

1. Installation environment

linux: centos 7.9
MySQL: 8.0.23
Installation directory: / opt/mysql (any directory can be used)

Attach the download address of MySQL official website: https://dev.mysql.com/downloads/mysql/5.7.html#downloads , the interface after entering is as follows. Click the "Archives" tab to select different versions. Mysql-8.0.23-linux-glibc2.0 is selected in this article 12-x86_ 64.tar. xz

2. Install mysql

After the software is downloaded, it needs to be transmitted to the target host through ftp and other tools. After the transmission is completed, the installation operation can be carried out. The following are the specific steps

1. First, unzip the software to a directory (the directory can be specified arbitrarily)

tar -xvf mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz -C /opt/ #Extract it to the / opt / directory
cd /opt/ #Enter the decompression directory
mv mysql-8.0.23-linux-glibc2.12-x86_64 mysql #Rename the extracted installation package to mysql. If the name is too long, it will be difficult to operate later. It is recommended to rename it
  1. Create a normal user. In fact, you do not need to log in to the user. It is mainly used to empower the installation directory of MySQL software. Because MySQL is initialized through the built-in script bin/mysqld, which requires a specified user to install. Now the login is the root user (generally, the software is not installed by root), and the owner of the created files is the root user. In order to install MySQL scripts with the privileges of ordinary users, you need to create an ordinary user mysql, and you need to manually assign the MySQL installation path to mysql. The weighting operation is as follows:
groupadd mysql #Create mysql user group
useradd -r -g mysql mysql #Create a mysql user, and the user mysql belongs to the mysql user group
chown -R mysql:mysql /opt/mysql #Give mysql folder mysql permission

3. Install mysql

It should be noted that at the beginning of mysql installation, you need to specify whether to ignore case, because once the installation is successful, you can no longer modify the policy. The strategy is also simple. Just add the parameter -- lower case table names = 1 after the mysql initialization command.

cd /opt/mysql8.0.23
bin/mysqld --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data/ --initialize --lower-case-table-names=1

If it has not been installed before, it can be initialized successfully (if the basic dependencies exist, it may be necessary to download some dependency packages on the Internet. The operating system in this paper has not been updated in any environment, but there is no lack of dependencies. Therefore, this paper will not explain this situation). The interface after success is as follows, The string in the red box is the login password of mysql. It's best to copy the password here, which will be used when logging in later. If the installation is unsuccessful, it may be because msyql has been installed before, or the installation directory remains. Please refer to 4.5 below for solutions.
4. Start mysql

After initializing mysql, MySQL will not start automatically and needs to be started manually.
This article starts MySQL using the script file / opt / MySQL / support files / MySQL Server, because the script will specify the installation path, we need to change the path where our installation package is located before starting/ opt/mysql/support-files/mysql. The server needs to be changed mainly from lines 66 to 73 in the file:

 62 # Set some defaults
 63 mysqld_pid_file_path=
 64 if test -z "$basedir"
 65 then
 66   basedir=/opt/mysql
 67   bindir=/opt/mysql/bin
 68   if test -z "$datadir"
 69   then
 70     datadir=/opt/mysql/data
 71   fi
 72   sbindir=/opt/mysql/bin
 73   libexecdir=/opt/mysql/bin
 74 else
 75   bindir="$basedir/bin"
 76   if test -z "$datadir"
 77   then
 78     datadir="$basedir/data"
 79   fi
 80   sbindir="$basedir/sbin"
 81   libexecdir="$basedir/libexec"
 82 fi

When starting mysql, the startup script will also go to / etc / my CNF's configuration file goes down to get parameters, so you also need to change / etc / my Some parameters in CNF configuration file.

  1 [mysqld]
  2 datadir=/opt/mysql/data  #
  3 socket=/opt/mysql/mysql.sock
  4 # Disabling symbolic-links is recommended to prevent assorted security risks
  5 symbolic-links=0
  6 # Settings user and group are ignored when systemd is used.
  7 # If you need to run mysqld under a different user or group,
  8 # customize your systemd unit file for mariadb according to the
  9 # instructions in http://fedoraproject.org/wiki/Systemd
 10
 11 lower-case-table-names=1 #ignore case
 12
 13 [mysqld_safe]
 14 log-error=/opt/mysql/data/err.log #Location of log files
 15 pid-file=/opt/mysql/data/mariadb.pid #Location of pid file
 16
 17 [client]
 18 socket=/opt/mysql/mysql.sock //Configuration of client login
 19
 20 #
 21 # include all files from the config directory
 22 #
 23 !includedir /etc/my.cnf.d

/etc/my. The configuration in CNF is important. If it is not configured correctly, the installation may not succeed. Errors in the installation process are posted at the end of the text. If similar errors occur, you can refer to them.

After confirming to modify the above two files, you can start mysql

 ./support-files/mysql.server start #mysql startup command

The interface after successful startup is as follows:

It can also be used/ support-files/mysql.server status view the status of MySQL

After MySQL is started successfully, you can use the script provided with the MySQL installation package (/ binmysql) to enter mysql

bin/mysql -uroot -p'Password at the beginning'


Since mysql commands are often used, you can link bin/mysql to the system's default / usr/bin path

ln -s /opt/mysql/bin/mysql /usr/bin
bin/mysql -uroot -p Password at the beginning

3. Verification

After entering mysql, enter show databases and the following error will appear:

This is because mysql requires mandatory password change when logging in for the first time. Use the following command to modify the initial password of mysql

alter user 'root'@'localhost' identified by "Your new password";

After changing the password, you can use mysql normally.

4. Error

There are various errors when starting mysql, most of which are caused by the incorrect configuration of the configuration file. Here are some errors during the installation of this article:
4.1 ERROR! The server quit without updating PID file (/opt/mysql/data/test02.pid).
This is a very common error. There are many reasons for the error, so you need to check the specific error information under the path of the installation log, cat / opt / MySQL / data / err log
It can be seen that there are two errors in the prompt. Solve the first one first:

Different lower_case_table_names settings for server ('0') and data dictionary ('1').

MySQL8.0 adds the concept of data dictionary. During data initialization, the parameter lower case table names = 0 is used by default under Linux, and my is read when the database is started The value in the CNF file. If the two values are inconsistent, the error message will be recorded in the error log of MySQL.
We have specified lower case table names = 0 during initialization, so for the sake of and my CNF is consistent, which needs to be in / etc / my Add lower case table names = 0 under mysqld of CNF


Restart MySQL:/ support-files/mysql.server status

It can start normally. Look at the log and find that both errors have disappeared

If the above steps are performed and there is still a Data Dictionary initialization failed error in the log, it should be caused by repeated setup. You can delete the entire data directory and try again. If this problem still occurs after deletion, it may be that there are installation programs in other places. If you can't find where these residual files are, it is recommended to delete the entire installation directory and reinstall. I have encountered this error several times during the installation process, which is solved in this way. However, if there is data in the data folder, it is recommended to determine which files are duplicated and then delete them, or find another way.
However, during the installation process, Data Dictionary initialization failed exists alone, which can also be solved by deleting the data directory.
There are many reasons for this error. When solving it, you should look at the log to judge which cause it and apply the right medicine to the case.

4.2 ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)


This is because / opt/mysql / support files / mysql.com has not been modified It is caused by the installation path in the server script file. When executing the self-contained script, we will go to the path specified by the script (/ usr/local/mysql/bin) to find what is needed for installation. Because we install it uniformly in the / opt/mysql directory, we will not find / usr/local/mysql/bin, so we need to modify the corresponding configuration file in the configuration file into a practical installation path.

66   basedir=/opt/mysql
 67   bindir=/opt/mysql/bin
 68   if test -z "$datadir"
 69   then
 70     datadir=/opt/mysql/data
 71   fi
 72   sbindir=/opt/mysql/bin
 73   libexecdir=/opt/mysql/bin
 74 else

4.3 mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
This is because the execution script jumps to / etc / my The default directory in CNF does not exist, so you need to modify the path/ etc/my. There are two places in CNF that affect the log file, line 2 and line 12

  1 [mysqld]
  2 datadir=/opt/mysql/data
  ......
 11 [mysqld_safe]
 12 log-error=/opt/mysql/data/err.log

4.4 Directory '/var/lib/mysql' for UNIX socket file don't exists.

Modify / etc / my The path of socket in CNF is OK

  1 [mysqld]
  2 datadir=/opt/mysql/data  #
  3 socket=/opt/mysql/mysql.sock

4.5[ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.

2021-07-01T18:30:35.220806Z 0 [ERROR] [MY-013236] [Server] The designated data directory /opt/mysql/data/ is unusable. You can remove all files that the server added to it.

This is because there are residual files in the data directory, which need to be deleted and restarted

Topics: Java Linux Database MySQL