1. 程式人生 > >MySQL 5.7.19 編譯安裝與配置

MySQL 5.7.19 編譯安裝與配置

進入MySQL官網下載頁面,地址https://www.mysql.com/downloads/,如果你想使用MySQL 5.7.19的原始碼版本,點此處直接下載!

進入MySQL Community Edition下載頁面
點選進入MySQL Community Edition下載頁面

點選DOWNLOAD

選擇作業系統為Source Code,選擇作業系統版本為Generic Linux,選擇Compressed TAR Archive, Includes Boost Headers版本或Compressed TAR Archive版本,暫未研究兩個版本的區別,開始以為Includes Boost Headers不用再去下載Boost庫,然而安裝時發現還是需要,所以此處先任意選擇一個版本,選擇點選 Download 進行下載

選擇原始碼下載

點選下載,此處並不需要註冊

進入/usr/local/src目錄,一般我喜歡把下載的檔案放在此目錄,根據自己的喜好設定

[[email protected] src]# cd /usr/local/src
  • 1

下載MySQL檔案,如果wget沒有安裝,yum -y install wget即可安裝

[[email protected] src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.19.tar.gz
  • 1

問題錯誤:
如果此處下載遇到如下問題,說明你沒有安裝openssl,此問題一般只會出現在全新的機器上

[[email protected] src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.19.tar.gz--2017-09-22 16:20:26-- https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.19.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. Unable to establish SSL connection.
  • 1

解決方案:

安裝opensll,yum -y install openssl 再次執行下載命令即可
  • 1

由於MySQL 5.7需要boost 1.59以及以上版本,所以還需要下載boost庫,根據本人測試1.59版本的最為適合,其它高版本在安裝的時候遇到了一些問題,目前未解決;下載地址,你也可以點選此處直接下載boost_1_59_0.tar.gz,如果wget無法下載,建議使用迅雷下載後再上傳到伺服器目錄

[[email protected] src]# wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
  • 1

解壓 boost並拷貝 boost 到 /usr/local/boost 目錄

[[email protected] src]# tar zxvf boost_1_59_0.tar.gz
[[email protected] src]# cp -r boost_1_59_0 /usr/local/boost
  • 1
  • 2

安裝編譯所需的常用元件和依賴包 [ 參考於網路部落格 ]

[[email protected] src]# yum -y install gcc gcc-c++ ncurses ncurses-devel bison libgcrypt perl make cmake
  • 1

建立mysql使用者組和使用者,用來執行mysql伺服器, -g指定使用者組, -r建立系統使用者

[[email protected] src]# groupadd mysql
[[email protected] src]# useradd -r -g mysql -s /bin/false -M mysql
  • 1
  • 2

解壓MySQL,進入 mysql-5.7.19 目錄

[[email protected] src]# tar zxvf mysql-boost-5.7.19.tar.gz 
[[email protected] src]# cd mysql-5.7.19/
  • 1
  • 2

新建MySQL安裝所需要目錄

[[email protected] mysql-5.7.19]# mkdir -p /usr/local/mysql /usr/local/mysql/{data,logs,pids}
  • 1

修改 /usr/local/mysql 目錄所有者許可權

[[email protected] mysql-5.7.19]# chown -R mysql:mysql /usr/local/mysql
  • 1

使用cmake命令進行編譯

[[email protected] mysql-5.7.19]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/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
  • 1

cmake執行完的結果

使用make命令進行編譯,接下來你將經歷漫長的等待~

[[email protected] mysql-5.7.19]# make
  • 1

問題錯誤:
編譯出現錯誤,查詢得知可能是由於記憶體不足導致的

c++編譯出錯

解決方案:
臨時增加交換空間 ( 虛擬記憶體 )

[[email protected] mysql-5.7.19]# dd if=/dev/zero of=/swapfile bs=1k count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 34.6782 s, 60.5 MB/s
[[email protected] mysql-5.7.19]# mkswap /swapfile
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=56026239-26e6-40d9-b080-b95acd9db058
[[email protected] mysql-5.7.19]# swapon /swapfile
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
[[email protected] mysql-5.7.19]# chmod 600 /swapfile
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

檢視建立的交換空間

[[email protected] mysql-5.7.19]# free -m
             total   used  free  shared  buff/cache  available
Mem:         992     51    70    0       869         789
Swap:        1999    0     1999
  • 1
  • 2
  • 3
  • 4

繼續執行make命令

[[email protected] mysql-5.7.19]# make clean
[[email protected] mysql-5.7.19]# make
  • 1
  • 2

如果你編譯完成後不再想要此交換空間,你可以執行如下命令:

[[email protected] mysql-5.7.19]# swapoff /swapfile
[[email protected] mysql-5.7.19]# rm /swapfile
  • 1
  • 2

溫馨提示:
MySQL編譯過程等待時間會比較久,有時都以為是“卡”住了,你可以使用top命令檢視資源狀態,看看cc1plus、make等程序是否在跳動,如果有跳動說明安裝還在繼續,由於我的 ecs 配置較低,此過程大約經歷了幾個小時,特別是在29%和74%的時候,幾乎都要快放棄了, 如果有經濟的能力的話,建議伺服器配置還是儘量買高一點。

[[email protected] mysql-5.7.19]# top
  • 1

make編譯完成
make編譯完成

編譯完成後執行 make install 進行安裝

[[email protected] mysql-5.7.19]# make install
  • 1

將mysql新增到環境變數,修改/etc/profile檔案,在檔案最末尾新增export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH

[[email protected] mysql-5.7.19]# vim /etc/profile
...
unset i
unset -f pathmunge
# mysql執行路徑
export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

更新配置檔案

[[email protected] mysql-5.7.19]# source /etc/profile
  • 1

初始化資料庫, –initialize 表示預設生成一個安全的密碼,–initialize-insecure 表示不生成密碼

[[email protected] mysql-5.7.19]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
  • 1

將mysql服務檔案拷貝到/etc/init.d/目錄,並給出執行許可權

[[email protected] mysql-5.7.19]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql-5.7.19]# chmod a+x /etc/init.d/mysqld
  • 1
  • 2

將MySQL並加入開機自動啟動

[[email protected] mysql-5.7.19]# chkconfig --add mysqld
[[email protected] mysql-5.7.19]# chkconfig mysqld on
[[email protected] mysql-5.7.19]# chkconfig --list | grep 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

修改/etc/my.cnf檔案,編輯配置檔案如下,僅供參考

[[email protected] mysql-5.7.19]# vim /etc/my.cnf 

[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock

[mysqld_safe]
log-error=/usr/local/mysql/logs/mysqld.log
pid-file=/usr/local/mysql/pids/mysqld.pid

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[client]
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock

[mysql]
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

啟動MySQL

[[email protected] local]# service mysqld start
Starting MySQL.2017-09-23T16:13:16.049373Z mysqld_safe error: log-error set to '/usr/local/mysql/logs/mysqld.log', however file don't exists. Create writable for user 'mysql'.
The server quit without updating PID file (/usr/local/mysql[FAILED]2864f6btwZ.pid).
  • 1
  • 2
  • 3

問題錯誤:
由於缺少 mysqld.log 和 mysqld.pid 檔案導致無法正常啟動
解決方案:
建立 mysqld.log 和 mysqld.pid 檔案

[[email protected] mysql-5.7.19]# touch /usr/local/mysql/logs/mysqld.log
[[email protected] mysql-5.7.19]# touch /usr/local/mysql/pids/mysqld.pid
  • 1
  • 2

修改 /usr/local/mysql 的許可權

[[email protected] mysql-5.7.19]# chown mysql.mysql -R /usr/local/mysql/
  • 1

再次啟動MySQL
[[email protected] local]# service mysqld start
檢視MySQL執行狀態

[[email protected] local]# service mysqld status
  • 1

MySQL is not running, but lock file (/var/lock/subsys/mysql[FAILED]

問題錯誤:

MySQL is not running, but lock file (/var/lock/subsys/mysql[FAILED]
  • 1

解決方案:
刪除/var/lock/subsys/mysql檔案,重新啟動MySQL

[[email protected] local]# rm -f /var/lock/subsys/mysql
[[email protected] local]# service mysqld start
Starting MySQL.                                            [  OK  ]
連線MySQL

[[email protected] mysql-5.7.19]# mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[[email protected]Z2864f6btwZ mysql-5.7.19]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

問題錯誤:
/etc/my.cnf 檔案配置不正確

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
  • 1

解決方案:
參考上述步驟 修改 /etc/my.cnf 檔案

[[email protected] local]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.19 Source distribution

Copyright (c) 2000, 2017, 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

檢視資料庫,如果看到以下幾個資料庫說明資料庫初始化成功

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

進入mysql庫,檢視使用者表資訊

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>
mysql> select host,user,password from user;
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
mysql>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

問題錯誤:

ERROR 1054 (42S22): Unknown column 'password' in 'field list'
  • 1

解決方案:
由於MySQL 5.7版本下的mysql資料庫下已經沒有password這個欄位了,password欄位改成了authentication_string,查詢時使用authentication_string欄位即可

mysql> select host,user,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host      | user          | authentication_string                     |
+-----------+---------------+-------------------------------------------+
| localhost | root          |                                           |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+---------------+-------------------------------------------+
3 rows in set (0.00 sec)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

設定密碼(推薦),注意此方法必須使用flush privileges命令重新整理一下許可權才能生效

mysql> UPDATE user SET authentication_string=PASSWORD('newpassword') WHERE user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

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

mysql>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

另外一種方法可以快速密碼,但此方法設定的密碼使用history命令可以看到,所以不太推薦

[[email protected] ~]# mysqladmin -u root password 'newpassword'
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.
  • 1
  • 2
  • 3

結尾:
至此,MySQL 5.7.19 編譯安裝及配置已經全部完成,有疑問的朋友可以給我留言,若有毛病,歡迎指正。