Linux-compile and install MySQL (5.7.32)

1. Check the system environment

[root@mysql ~]# uname  -r
3.10.0-957.el7.x86_64
[root@mysql ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

2. Uninstall mariadb

PS: Because the centos7 version has mariadb installed by default, it needs to be uninstalled in advance.

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

3. Install dependencies

Install dependent software

[root@mysql ~]# yum -y install openssl openssl-devel gcc gcc-c++  m4

cmake

PS: Since the conventional configure compilation method has been abandoned since MySQL 5.5, a CMake compiler is required to set the compilation parameters of mysql. Such as: installation directory, data storage directory, character encoding, sorting rules, etc.

[root@mysql ~]# wget http://www.cmake.org/files/v3.18/cmake-3.18.1.tar.gz 
[root@mysql ~]# tar zxf cmake-3.18.1.tar.gz 
[root@mysql ~]# cd cmake-3.18.1/
[root@mysql cmake-3.18.5]# ./bootstrap && gmake && gmake install
[root@mysql cmake-3.18.5]# cmake  -version
cmake version 3.18.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

ncurses

PS: Character terminal processing library

[root@mysql ~]# wget ftp://ftp.gnu.org/gnu/ncurses/ncurses-6.2.tar.gz
[root@mysql ~]# tar zxf ncurses-6.2.tar.gz 
[root@mysql ~]# cd ncurses-6.2/
[root@mysql ncurses-6.2]# ./configure  && make && make install

bison

PS: C/C++ syntax analyzer under Linux

[root@mysql ~]# wget http://ftp.gnu.org/gnu/bison/bison-3.6.3.tar.gz
[root@mysql ~]# tar zxf  bison-3.6.3.tar.gz 
[root@mysql ~]# cd bison-3.6.3/
[root@mysql bison-3.6.3]# ./configure  && make && make install

bootst

PS: Starting from MySQL 5.7.5, the Boost library is necessary. The C++ Boost library is used in the mysql source code. Boost 1.59.0 or above must be installed. GCC is a C language compilation tool under Linux. The mysql source code is compiled entirely by C and Written in C++, GCC must be installed

[root@mysql ~]# wget http://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
[root@mysql ~]# tar zxf boost_1_59_0.tar.gz 
[root@mysql ~]# mv boost_1_59_0 /usr/local/boost

4. Create users and groups and directories

[root@mysql ~]# groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql  #新建 msyql 组和 msyql 用户禁止登录 shell
[root@mysql ~]# mkdir /usr/local/mysql   #创建目录
[root@mysql ~]# mkdir /usr/local/mysql/data  #创建数据库目录

5. Compile and install MySQL

Unzip and install

[root@mysql ~]# wget http://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.32.tar.gz
[root@mysql ~]# tar zxf mysql-5.7.32.tar.gz 
[root@mysql ~]# cd mysql-5.7.32/
[root@mysql mysql-5.7.32]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  -DMYSQL_DATADIR=/usr/local/mysql/data  -DSYSCONFDIR=/etc    -DWITH_MYISAM_STORAGE_ENGINE=1  -DWITH_INNOBASE_STORAGE_ENGINE=1  -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock  -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all  -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci   -DWITH-SYSTEMD=1   -DWITH_BOOST=/usr/local/boost && make -j 4 && make install
parameter meaning
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql Root directory of MySQL installation
-DMYSQL_DATADIR=/usr/local/mysql/data MySQL database file storage directory
-DSYSCONFDIR=/etc The directory where the MySQL configuration file is located
-DWITH_MYISAM_STORAGE_ENGINE=1 Add MYISAM engine support
-DWITH_INNOBASE_STORAGE_ENGINE=1 Add InnoDB engine support
-DWITH_ARCHIVE_STORAGE_ENGINE=1 Add ARCHIVE engine support
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock Specify mysql.sock location
-DWITH_PARTITION_STORAGE_ENGINE=1 Install support database partition
-DEXTRA_CHARSETS=all Make MySQL support all extended characters
-DDEFAULT_CHARSET=utf8 Set MySQL's default character set to utf8
-DDEFAULT_COLLATION=utf8_general_ci Set the default character set collation rules
-DWITH-SYSTEMD = 1 You can use systemd to control the mysql service
-DWITH_BOOST=/usr/local/boost Point to the directory where the boost library is located
make  -j  4 && make install
  • -j: Specifies the number of threads during compilation according to the number of CPU cores, which can speed up compilation. The default is 1 thread compilation.

Optimization path

[root@mysql ~]# tail -1 /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
[root@mysql ~]# source /etc/profile

6. Create a configuration file

[root@mysql ~]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql    #创建目录
datadir=/usr/local/mysql/data    #数据库目录
pid-file=/usr/local/mysql/data/mysqld.pid   #mysql进程
log-error=/usr/local/mysql/data/mysql.err   #错误日志
socket=/tmp/mysql.sock  #可以socket文件方式登录mysql

7. Set permissions and initialize MySQL

[root@mysql ~]# chown -R mysql:mysql /usr/local/mysql/
[root@mysql mysql-5.7.32]# ll /usr/local/mysql/
总用量 268
drwxr-xr-x  2 mysql mysql   4096 11月 19 22:07 bin
drwxr-xr-x  2 mysql mysql      6 11月 19 21:53 data
drwxr-xr-x  2 mysql mysql     55 11月 19 22:07 docs
drwxr-xr-x  3 mysql mysql   4096 11月 19 22:07 include
drwxr-xr-x  4 mysql mysql    192 11月 19 22:07 lib
-rw-r--r--  1 mysql mysql 247914 9月  23 20:00 LICENSE
drwxr-xr-x  4 mysql mysql     30 11月 19 22:07 man
drwxr-xr-x 10 mysql mysql   4096 11月 19 22:07 mysql-test
-rw-r--r--  1 mysql mysql    587 9月  23 20:00 README
-rw-r--r--  1 mysql mysql    587 9月  23 20:00 README-test
drwxr-xr-x 28 mysql mysql   4096 11月 19 22:07 share
drwxr-xr-x  2 mysql mysql     90 11月 19 22:07 support-files
drwxr-xr-x  3 mysql mysql     17 11月 19 22:07 usr
[root@mysql mysql-5.7.32]#  mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
  • To initialize the operation as root, add the -user=mysql parameter to generate a random password (note that it is used when saving the login)
  • If you use the -initialize parameter to initialize the system database, a temporary password for the root user will be generated

8. Start MySQL

[root@mysql ~]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service  /usr/lib/systemd/system/
[root@mysql ~]# vim /usr/lib/systemd/system/mysqld.service 
......
ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd   #修改为自己设置的mysql路径

ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/data/mysqld.pid $MYSQLD_OPTS   ##修改为自己设置的mysql路径
......
[root@mysql ~]# systemctl  daemon-reload 
[root@mysql ~]# systemctl  start  mysqld.service 
[root@mysql ~]# systemctl  enable  mysqld.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.

[root@mysql ~]# ss -lnt | grep  3306
LISTEN     0      80          :::3306                    :::*  

9. Log in to MySQL

[root@mysql ~]# cat /usr/local/mysql/data/mysql.err  | grep password
2020-11-19T14:09:35.532890Z 1 [Note] A temporary password is generated for root@localhost: fFqNp#ty>6/?
[root@mysql ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.32

Copyright (c) 2000, 2020, 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> alter  user  root@localhost  identified  by '123456';
Query OK, 0 rows affected (0.00 sec)

change Password

[root@mysql ~]# mysqladmin  -u root -p password '123456'
Enter password: 123456
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

Guess you like

Origin blog.csdn.net/weixin_45191791/article/details/110136458