MySQL5.7をコンパイルしてインストールする

MySQL5.7をコンパイルしてインストールする

オペレーティングシステム:CentOS7、オペレーティングシステムのバージョンは次のとおりです。

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

ステップ1、必要な依存関係をインストールする

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

ステップ2、MySQ5.7ソースパッケージをダウンロードする

[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

ステップ3、ユーザーとグループを作成する

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

ステップ4、MySQLをコンパイルしてインストールする

1. MySQLパッケージを解凍します

[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. boost_1_59_0.tar.gzをダウンロードします

MySQL 5.7.5以降、Boostライブラリが必要です。バージョンは1.59で、boost_1_59_0.tar.gzとMySQLソースコードパッケージは同じディレクトリに配置されます。

[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. MySQLをコンパイルします

(1)cmakeコマンドを実行します。

[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

パラメータの説明:

(1)DCMAKE_INSTALL_PREFIX:MySQLインストールパスを指定します;
(2)DMYSQL_DATADIR:MySQLデータファイルのパスを指定します;
(3)DMYSQL_UNIX_ADDR:sockファイルのパスを指定します;
(4)DSYSCONFDIR:設定ファイルのパスを指定します;
(5)DWITH_MYISAM_STORAGE_ENGINE = 1: myisamストレージエンジンをインストールします;
(6)DWITH_INNOBASE_STORAGE_ENGINE = 1:innodbストレージエンジンをインストールします;
(7)DDEFAULT_CHARSET:デフォルトの文字セットを設定します;
(8)DDEFAULT_COLLATION:デフォルトの照合セットを設定します;
(9)DEXTRA_CHARSETS:すべての拡張文字セットをインストールします;
(10 )DMYSQL_USER:MySQLを操作するユーザーを指定します。

結果は次のとおりです。

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

-- 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

コンパイル中にエラーが発生した場合は、CMakeCache.txtを削除して再コンパイルします。

rm -rf CMakeCache.txt

4. makeコマンドを実行してコンパイルします

[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. make 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インストールディレクトリ:

[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

ステップ5、MySQLの初期設定

1.必要に応じて構成ファイルを生成します

[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.環境変数を構成する

[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.データファイルを初期化する

## 更改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. MySQL起動スクリプトを構成する

[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

ステップ6、ログインアカウントとパスワードを設定する

1.コンパイルしてインストールした後、デフォルトでパスワードなしでログインできます

[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. MySQLユーザー情報を表示する

rootアカウントにはパスワードがないことがわかります。

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. rootアカウントのログインパスワードを設定します

必要に応じて他のアカウントを追加することもできます。

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. MySQLに再度ログインします

ログイン成功!

[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> 

おすすめ

転載: blog.csdn.net/weixin_44377973/article/details/107523225