Compile and install MySQL5.7

Compile and install MySQL5.7

Operating system: CentOS7, the operating system version is as follows:

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)

step1, install the required dependencies

yum install -y  gcc gcc-c++ cmake ncurses ncurses-devel bison

step2, download the MySQ5.7 source package

[root@localhost ~]# mkdir /mysoft/mysql
[root@localhost ~]# cd /mysoft/mysql
[root@localhost mysql]# pwd
/mysoft/mysql

[root@localhost mysql]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.23.tar.gz

--2020-07-13 10:34:59--  https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.23.tar.gz
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://cdn.mysql.com//archives/mysql-5.7/mysql-boost-5.7.23.tar.gz [following]
--2020-07-13 10:35:01--  https://cdn.mysql.com//archives/mysql-5.7/mysql-boost-5.7.23.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 184.29.107.217
Connecting to cdn.mysql.com (cdn.mysql.com)|184.29.107.217|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 49025014 (47M) [application/x-tar-gz]
Saving to: ‘mysql-boost-5.7.23.tar.gz’

13% [==========>                                                                            ] 6,497,094    390

step3, create users and groups

[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# useradd mysql -s /sbin/nologin -M -g mysql

step4, compile and install MySQL

1. Unzip the MySQL package

[root@localhost mysql]# tar zxf mysql-boost-5.7.23.tar.gz 
[root@localhost mysql]# ll -h
total 47M
drwxr-xr-x. 36 7161 31415 4.0K Jun  8  2018 mysql-5.7.23
-rw-r--r--.  1 root root   47M Jun  8  2018 mysql-boost-5.7.23.tar.gz

2. Download boost_1_59_0.tar.gz

Starting from MySQL 5.7.5, the Boost library is required, the version is 1.59, and the boost_1_59_0.tar.gz and MySQL source code packages are placed in the same directory.

[root@localhost mysoft]# wget --no-check-certificate http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz

100%[=================================>] 83,709,983  1.81MB/s   in 18s    
2020-07-13 21:42:34 (1.81 MB/s) - ‘boost_1_59_0.tar.gz’ saved [83709983/83709983]

[root@localhost mysql]# tar zxf boost_1_59_0.tar.gz 
[root@localhost mysoft]# pwd
/mysoft
[root@localhost mysoft]# ll
total 129636
drwx------.  8  501 games     4096 Aug 11  2015 boost_1_59_0
-rw-r--r--.  1 root root  83709983 Aug 13  2015 boost_1_59_0.tar.gz
drwxr-xr-x. 36 7161 31415     4096 Jun  8  2018 mysql-5.7.23
-rw-r--r--.  1 root root  49025014 Jun  8  2018 mysql-boost-5.7.23.tar.gz

3. Compile MySQL

(1) Execute the cmake command:

[root@localhost mysoft]# cd mysql-5.7.23/

[root@localhost mysoft]#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7 \
-DMYSQL_DATADIR=/usr/local/mysql-5.7/data \
-DMYSQL_UNIX_ADDR=/usr/local/mysql-5.7/tmp/mysql.sock \
-DSYSCONFDIR=/usr/local/mysql-5.7/conf \
-DSYSTEMD_PID_DIR=/usr/local/mysql-5.7/tmp \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_USER=mysql \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DEXTRA_CHARSETS=all \
-DENABLE_LOCAL_INFILE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_BOOST=../boost_1_59_0

Parameter Description:

(1) DCMAKE_INSTALL_PREFIX: specify the MySQL installation path;
(2) DMYSQL_DATADIR: specify the path of the MySQL data file;
(3) DMYSQL_UNIX_ADDR: specify the path of the sock file;
(4) DSYSCONFDIR: specify the configuration file path;
(5) DWITH_MYISAM_STORAGE_ENGINE=1: Install the myisam storage engine;
(6) DWITH_INNOBASE_STORAGE_ENGINE=1: install the innodb storage engine;
(7) DDEFAULT_CHARSET: set the default character set;
(8) DDEFAULT_COLLATION: set the default collation set;
(9) DEXTRA_CHARSETS: install all extended character sets;
(10 ) DMYSQL_USER: Specify the user who operates MySQL;

The results are as follows:

......................

-- CMAKE_C_LINK_FLAGS: 
-- CMAKE_CXX_LINK_FLAGS: 
-- CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
####################  出现以下两行表示  成功生成编译环境###############################
-- Configuring done     
-- Generating done      
#################################################################################
CMake Warning:
  Manually-specified variables were not used by the project:

    ENABLE_LOCAL_INFILE
    MYSQL_USER
    SYSTEMD_PID_DIR


-- Build files have been written to: /mysoft/mysql-5.7.23

If an error occurs during compilation, delete CMakeCache.txt and recompile:

rm -rf CMakeCache.txt

4. Execute the make command to compile

[root@localhost mysql-5.7.23]# make
[  0%] Built target INFO_BIN
[  0%] Built target INFO_SRC

......

Scanning dependencies of target mysqltest_embedded
[100%] Building CXX object libmysqld/examples/CMakeFiles/mysqltest_embedded.dir/__/__/client/mysqltest.cc.o
Linking CXX executable mysqltest_embedded
[100%] Built target mysqltest_embedded
Scanning dependencies of target my_safe_process
[100%] Building CXX object mysql-test/lib/My/SafeProcess/CMakeFiles/my_safe_process.dir/safe_process.cc.o
Linking CXX executable my_safe_process
[100%] Built target my_safe_process

5. Execute the make install command to install MySQL

[root@localhost mysql-5.7.23]# make install

.......

-- Installing: /usr/local/mysql-5.7/mysql-test/lib/My/SafeProcess/my_safe_process
-- Up-to-date: /usr/local/mysql-5.7/mysql-test/lib/My/SafeProcess/my_safe_process
-- Installing: /usr/local/mysql-5.7/mysql-test/lib/My/SafeProcess/Base.pm
-- Installing: /usr/local/mysql-5.7/support-files/mysqld_multi.server
-- Installing: /usr/local/mysql-5.7/support-files/mysql-log-rotate
-- Installing: /usr/local/mysql-5.7/support-files/magic
-- Installing: /usr/local/mysql-5.7/share/aclocal/mysql.m4
-- Installing: /usr/local/mysql-5.7/support-files/mysql.server

MySQL installation directory after installation:

[root@localhost mysql-5.7.23]# ll /usr/local/mysql-5.7/
total 64
drwxr-xr-x.  2 root root  4096 Jul 13 23:01 bin
-rw-r--r--.  1 root root 17987 Jun  8  2018 COPYING
-rw-r--r--.  1 root root 17987 Jun  8  2018 COPYING-test
drwxr-xr-x.  2 root root    55 Jul 13 23:00 docs
drwxr-xr-x.  3 root root  4096 Jul 13 23:00 include
drwxr-xr-x.  4 root root   192 Jul 13 23:01 lib
drwxr-xr-x.  4 root root    30 Jul 13 23:00 man
drwxr-xr-x. 10 root root  4096 Jul 13 23:02 mysql-test
-rw-r--r--.  1 root root  2478 Jun  8  2018 README
-rw-r--r--.  1 root root  2478 Jun  8  2018 README-test
drwxr-xr-x. 28 root root  4096 Jul 13 23:02 share
drwxr-xr-x.  2 root root    90 Jul 13 23:02 support-files

step5, initial configuration MySQL

1. Generate configuration files as needed

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


[client]
#password = your_password
port = 3306
socket = /usr/local/mysql/tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/mysql.pid
log_error = /usr/local/mysql/mysql-error.log
socket = /usr/local/mysql/tmp/mysql.sock

2. Configure environment variables

[root@localhost etc]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
[root@localhost etc]# tail -l /etc/profile
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
export PATH=/usr/local/mysql/bin:$PATH
[root@localhost etc]# source /etc/profile
[root@localhost etc]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

3. Initialize the data file

## 更改MySQL安装目录和数据目录的权限
[root@localhost etc]# mkdir -p /usr/local/mysql/data/
[root@localhost etc]# chown -R mysql.mysql /usr/local/mysql
[root@localhost etc]# chown -R mysql.mysql /usr/local/mysql-5.7
[root@localhost etc]# chmod -R 1777 /tmp

[root@localhost bin]# pwd
/usr/local/mysql/bin

[root@localhost bin]# ./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

4. Configure MySQL startup script

[root@localhost support-files]# cp ./mysql.server /etc/init.d/mysqld

[root@localhost support-files]# chmod 700 /etc/init.d/mysqld
[root@localhost support-files]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS! 

##  设置MySQL开机自动启动
[root@localhost support-files]# chkconfig mysqld on
[root@localhost support-files]# chkconfig --list mysqld

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]'.

mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off

step6, set login account and password

1. After compiling and installing, you can log in without a password by default

[root@localhost support-files]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23 Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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> 

2. View MySQL user information

It can be seen that the root account has no password.

mysql> select host,user,authentication_string from mysql.user;
+-----------+---------------+-------------------------------------------+
| host      | user          | authentication_string                     |
+-----------+---------------+-------------------------------------------+
| localhost | root          |                                           |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+---------------+-------------------------------------------+
3 rows in set (0.00 sec)

3. Set a login password for the root account

You can also add other accounts as needed.

mysql> alter user root@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)

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

mysql> select host,user,authentication_string from mysql.user;
+-----------+---------------+-------------------------------------------+
| host      | user          | authentication_string                     |
+-----------+---------------+-------------------------------------------+
| localhost | root          | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+---------------+-------------------------------------------+
3 rows in set (0.00 sec)

4. Log in to MySQL again

login successful!

[root@localhost support-files]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.23 Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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> 

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/107523225