Detailed explanation of MySQL database compilation and installation under Linux

This article is simultaneously published on my WeChat official account. Scan the QR code at the bottom of the article or search chaodev on WeChat to follow it.


1. System environment

Hardware: Huawei domestic server (arm architecture Kunpeng CPU)
System: domestic bid-winning Kylin (64-bit)
MySQL version: 5.7.26
Insert picture description here

Disk and memory
Insert picture description here

Special note: The CPU of this server is of arm architecture, and those installation packages on the mysql official website will not work. Binary files cannot be executed during installation, as follows
Insert picture description here

We need to download the source code package ourselves to compile and install.


2. Preparation before installation

Some of the winning Kylin operating system may have MySQL pre-installed. If it has been installed, it needs to be uninstalled first, and /etc/my.cnf already exists. When installing, execute the following commands first, otherwise an error will be reported during installation.

mv /etc/my.cnf /etc/my.cnf.bak

Three, installation steps

1. Boost download

Why install boost? Because cmake MySQL source code package needs to be used, MySQL 5.7 requires boost 1.59 and above. It is recommended to use version 1.59.

Download link: https://www.boost.org/users/history/version_1_59_0.html
Insert picture description here


2. Boost installation

Unzip boost and copy it to the /usr/local/boost directory

tar -zxvf boost_1_59_0.tar.gz
cp -r boost_1_59_0 /usr/local/boost

installation

cd /usr/local/boost
./bootstrap.sh

Insert picture description here

Build boost library

./b2
./b2 --prefix=/usr/local/boost install

Note: bootstrap.sh is used to check the installation environment. b2 is generated after the previous step is successful, use it to build the boost library.


3. MySQL download

Download link: https://downloads.mysql.com/archives/community/
Insert picture description here

Download the source package, because we have to compile it ourselves.


4. MySQL installation

4.1 Unzip
tar -zxvf mysql-5.7.26.tar.gz

Copy to /usr/local/

cp mysql-5.7.26 /usr/local/

4.2 Create mysql user group and user

First check whether the mysql user group and user exist

cat /etc/group | grep mysql
cat /etc/passwd |grep mysql

Create mysql user group

groupadd mysql

Create user mysql and join the mysql user group

useradd -r -g mysql mysql

Change the user groups and users, and permissions of all directories and folders under the mysql directory

[root@localhost /]# chown -R mysql:mysql /usr/local/mysql-5.7.26
[root@localhost /]# chmod -R 755 /usr/local/mysql-5.7.26

4.3 Install the required components and dependencies for compilation
yum -y install gcc gcc-c++ ncurses ncurses-devel bison libgcrypt perl make cmake
yum -y install ncurses-devel libaio-devel bison perl-Time-HiRes
yum install libatomic

4.4 Create a data directory
mkdir -p /home/mysql/data              #创建目录
chown mysql:mysql -R /home/mysql/data   #赋予权限

4.5 Compile

Cut to the directory mysql-5.7.26 and use the cmake command to compile

cd mysql-5.7.26/
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.26 -DMYSQL_DATADIR=/home/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DENABLE_DOWNLOADS=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost

Then use the make command to compile

make

Insert picture description here

Note: If an error is reported and the instruction cannot be separated, please see the solution at the end of the article.


4.6 Installation

Execute make install to install

make install

Insert picture description here


5. Environment variable configuration

vi /etc/profile

Add at the end of the text

export PATH=/usr/local/mysql-5.7.26/bin:/usr/local/mysql-5.7.26/lib:$PATH

Insert picture description here

Run the following command to make the configuration effective

source /etc/profile

6. Create the required directory for the my.cnf file

mkdir /usr/local/mysql-5.7.26/tmp
mkdir /usr/local/mysql-5.7.26/run
chmod -R 755 /usr/local/mysql-5.7.26/tmp
chmod -R 755 /usr/local/mysql-5.7.26/run

Create mysqld.log and mysqld.pid files

mkdir /usr/local/mysql-5.7.26/logs
mkdir /usr/local/mysql-5.7.26/pids
touch /usr/local/mysql-5.7.26/logs/mysqld.log
touch /usr/local/mysql-5.7.26/pids/mysqld.pid

Insert picture description here


7. Configure the my.cnf file

At the beginning, mv was performed on my.cnf, and it can be restored at this time

cd /etc/
cp my.cnf.bak my.cnf

Insert picture description here

The revised content is as follows:

[mysqld_safe]
log-error=/usr/local/mysql-5.7.26/logs/mysqld.log
pid-file=/usr/local/mysql-5.7.26/pids/mysqld.pid
[mysqldump]
quick
[mysql]
no-auto-rehash
[client]
default-character-set=utf8
[mysqld]
basedir=/usr/local/mysql-5.7.26
tmpdir=/usr/local/mysql-5.7.26/tmp
datadir=/home/mysql/data
socket=/usr/local/mysql-5.7.26/run/mysql.sock
port=3306
user=root
#忽略大小写
lower_case_table_names=1

8. Database initialization

cd /usr/local/mysql-5.7.26/bin
mysqld --defaults-file=/etc/my.cnf --initialize --basedir=/usr/local/mysql-5.7.26 --datadir=/home/mysql/data --user=mysql

Insert picture description here

–Initialize means to generate a secure password by default, –initialize-insecure means not to generate a password.

After initialization, there will be a line of prompt, after the colon is the initial password, for example

2020-05-29T08:47:34.659979Z 1 [Note] A temporary password is generated for root@localhost: wpDnIn.du1ce

The initial password here is: wpDnIn.du1ce


9. Start MySQL

Copy the mysql service file to the /etc/init.d/ directory and give the execution permission

cp support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld

Insert picture description here

Start MySQL

service mysqld start

Four, related use

1. Change the password

Log in to mysql

/usr/local/mysql-5.7.26/bin/mysql -uroot -p -S /usr/local/mysql-5.7.26/run/mysql.sock

change Password

mysql>set password for root@localhost = password('123456');

2. Open remote connection

mysql>use mysql;
mysql>select Host,User,authentication_string from user;
msyql>update user set user.Host='%' where user.User='root';
mysql>flush privileges;

Five, firewall configuration

If the firewall is not open, open the firewall and start the firewall service, as follows

systemctl start firewalld

The default port of mysql is 3306, which needs to be opened, as follows

#永久开放3306端口
firewall-cmd --add-port=3306/tcp --permanent --zone=public
#重启防火墙
firewall-cmd --reload

Six, problems and solutions

1. Binary files cannot be executed during database initialization

[root@localhost bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/data
-bash: ./mysqld: 无法执行二进制文件

Insert picture description here

Reason: the installation package does not apply to this cpu kernel architecture.
Solution: need to recompile the mysql source package


2. An error is reported when compiling the installation package, prompting that the instruction cannot be separated

Insert picture description here

Solution: switch to the source package and modify the following file

vi storage/innobase/CMakeLists.txt

Insert picture description here

Modify the content of this paragraph as follows:

IF(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  # Bug was fixed in GCC 5.2, so workaround only needed < 5.2
  EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} -dumpversion
                  OUTPUT_VARIABLE GCC_VERSION)
  IF(GCC_VERSION VERSION_LESS 5.2)
    INCLUDE(${MYSQL_CMAKE_SCRIPT_DIR}/compile_flags.cmake)
    ADD_COMPILE_FLAGS(
      btr/btr0btr.cc
      btr/btr0cur.cc
      buf/buf0buf.cc
      gis/gis0sea.cc
      handler/handler0alter.cc
      mtr/mtr0mtr.cc
      row/row0merge.cc
      row/row0mysql.cc
      row/row0trunc.cc
      srv/srv0srv.cc
      fts/fts0fts.cc
      COMPILE_FLAGS "-O0"
      )
  ENDIF()
ENDIF()

Insert picture description here


3. Start mysql and report an error

Insert picture description here

Check that there is no such pid file
Insert picture description here

Then use the service mysql status command to view the startup status of mysql. The error is as follows:

[root@localhost data]# service mysqld status
 ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists

Insert picture description here

Find and delete this mysql file.



Like it if it helps! ! !
The original is not easy, please indicate the source for reprinting.

Scan the QR code below on WeChat to follow my official account

Guess you like

Origin blog.csdn.net/xch_yang/article/details/114916882