CentOS7.4 源碼安裝mysql-5.7.21

yum安裝見我的另一篇文章:http://www.itdecent.cn/p/0680bf8fa2f9

一、準(zhǔn)備工作(安裝mysql安裝依賴的環(huán)境)

1、Mysql5.7版本更新后有很多變化,安裝必須要BOOST庫(版本需為1.59.0)

1)boost庫下載地址:https://sourceforge.net/projects/boost/files/boost/1.59.0/

[root@VM download]# wget https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

2)檢查文件MD5值是否匹配:若不匹配需重新下載

[root@VM download]# md5sum boost_1_59_0.tar.gz 
51528a0e3b33d9e10aaa311d9eb451e3  boost_1_59_0.tar.gz

3)解壓后放在指定位置備用

[root@VM download]# tar -vxzf boost_1_59_0.tar.gz 
[root@VM download]# mv boost_1_59_0 /usr/local/boost_1_59_0
2、安裝依賴包
yum -y install gcc gcc-c++ ncurses ncurses-devel cmake

二、下載mysql + 編譯安裝

1、官網(wǎng)下載地址:https://dev.mysql.com/downloads/mysql/

1)下載文件
[root@VM download]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21.tar.gz
2)檢查文件MD5值是否匹配:若不匹配需重新下載
[root@VM download]# md5sum mysql-5.7.21.tar.gz 
e26523b174bdc3fd0fde6f36791ce17e  mysql-5.7.21.tar.gz
3)解壓文件
[root@VM download]# tar -vxzf mysql-5.7.21.tar.gz 

2、編譯和安裝

1)進(jìn)入到解壓文件目錄,依次運(yùn)行以下命令:
[root@VM mysql-5.7.21]# cmake . \
 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
 -DMYSQL_DATADIR=/data/mysql \
 -DWITH_BOOST=/usr/local/boost_1_59_0 \
 -DSYSCONFDIR=/etc \
 -DDEFAULT_CHARSET=utf8mb4 \
 -DDEFAULT_COLLATION=utf8mb4_general_ci \
 -DENABLED_LOCAL_INFILE=1 \
 -DEXTRA_CHARSETS=all

[root@VM mysql-5.7.21]# make        #時(shí)間會(huì)很長可能大于1小時(shí)
[root@VM mysql-5.7.21]# make install
  • -DCMAKE_INSTALL_PREFIX:mysql安裝目錄
  • -DMYSQL_DATADIR:數(shù)據(jù)存放目錄
  • -DWITH_BOOST:boost源碼路徑
  • -DSYSCONFDIR:my.cnf配置文件目錄
  • -DEFAULT_CHARSET:數(shù)據(jù)庫默認(rèn)字符編碼
  • -DDEFAULT_COLLATION:默認(rèn)排序規(guī)則
  • -DENABLED_LOCAL_INFILE:允許從本文件導(dǎo)入數(shù)據(jù)
  • -DEXTRA_CHARSETS:安裝所有字符集
    注:更多預(yù)編譯配置參數(shù)請參考mysql官方文檔說明:http://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html#cmake-general-options
2)錯(cuò)誤解決 -- make 時(shí)報(bào)錯(cuò):


主要原因大體上是因?yàn)閮?nèi)存不足,有點(diǎn)坑 臨時(shí)使用交換分區(qū)來解決吧

sudo dd if=/dev/zero of=/swapfile bs=64M count=16
sudo mkswap /swapfile
sudo swapon /swapfile

編譯出錯(cuò)了,需根據(jù)下面操作,重新編譯安裝。(網(wǎng)上搜到的方法,但是刪除后也沒有解決問題,最后將整個(gè)文件都刪除了,重新解壓了一份)

[root@VM mysql-5.7.21] make clean
[root@VM mysql-5.7.21] rm -f CMakeCache.txt
[root@VM mysql-5.7.21] make && make install

在編譯完成后,需運(yùn)行以下代碼:

sudo swapoff /swapfile
sudo rm /swapfile

三、配置MySQL

1、設(shè)置權(quán)限

1)使用下面的命令查看是否有mysql用戶及用戶組
cat /etc/passwd 查看用戶列表
cat /etc/group  查看用戶組列表
2)如果沒有就創(chuàng)建mysql數(shù)據(jù)庫管理用戶和組
[root@VM download]# groupadd mysql
[root@VM download]# useradd -r -g mysql -s /bin/false mysql
3)修改/usr/local/mysql權(quán)限
chown -R mysql:mysql /usr/local/mysql

2、修改/etc/my.cnf文件和/etc/my.cnf.d/mysql-clients.cnf文件

1)修改/etc/my.cnf文件
### /etc/my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock

#指定server端字符集
character-set-server = utf8 
collation-server = utf8_general_ci

# 解決問題:TIMESTAMP with implicit DEFAULT value is deprecated
explicit_defaults_for_timestamp=true

# mysql程序安裝目錄
basedir=/usr/local/mysql

# mysql數(shù)據(jù)文件目錄
datadir=/data/mysql

pid-file = /data/mysql/mysql.pid
log_error = /data/mysql/mysql-error.log

# 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

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

#更改字符集為utf-8
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8
2)修改/etc/my.cnf.d/mysql-clients.cnf文件
###  /etc/my.cnf.d/mysql-clients.cnf
#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#

# 在配置文件中添加“[client]”選項(xiàng)和“[mysql]”選項(xiàng)
# 并使用這兩個(gè)選項(xiàng)下的“socket”參數(shù)值,與“[mysqld]”選項(xiàng)下的“socket”參數(shù)值,指向的socket文件路徑完全一致
# 解決問題: connect to local MySQL server through socket /var/lib/mysql/mysql.sock

[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock

[client]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock

[mysql_upgrade]
[mysqladmin]
[mysqlbinlog]
[mysqlcheck]
[mysqldump]
[mysqlimport]
[mysqlshow]
[mysqlslap]

四、初始化數(shù)據(jù)庫并將mysql加入系統(tǒng)服務(wù)

1、初始化數(shù)據(jù)庫

[root@VM mysql-5.7.21]# cd /usr/local/mysql/bin/
#兩種方式任選一種
#1)不生成root密碼
[root@VM bin]# ./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql  

#2)生成root隨機(jī)密碼,在/data/mysql/mysql-error.log文件中 
[root@VM bin]#  ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql 

2、將mysql加入系統(tǒng)服務(wù)

[root@VM bin]#  cd /usr/local/mysql/support-files/
[root@VM support-files]#  cp mysql.server /etc/init.d/mysql 
[root@VM support-files]#  chkconfig --add mysql       # mysql加入系統(tǒng)服務(wù)
[root@VM support-files]#  chkconfig mysql on         # 開機(jī)啟動(dòng)
[root@VM support-files]#  service mysql start
運(yùn)行service mysql start報(bào)錯(cuò)
1)mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'. ERROR! The server quit without updating PID file (/data/mysql/mysql.pid).

解決方法:

[root@VM support-files]# mkdir /var/log/mariadb
[root@VM support-files]# touch /var/log/mariadb/mariadb.log
[root@VM support-files]# chown -R mysql:mysql /var/log/mariadb/
2)mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists. ERROR! The server quit without updating PID file (/data/mysql/mysql.pid).

解決方法:

[root@VM support-files]# mkdir /var/lib/mysql
[root@VM support-files]# chown -R mysql:mysql /var/lib/mysql/
解決了這兩個(gè)問題就啟動(dòng)成功了:
[root@VM support-files]# service mysql start                 
Starting MySQL. SUCCESS! 

mysql服務(wù)的啟動(dòng)/重啟/停止

service mysql start     #啟動(dòng)mysql服務(wù)
service mysql restart   #重啟mysql服務(wù)
service mysql stop      #停止mysql服務(wù)

五、登錄mysql

1、環(huán)境變量PATH,要不不能直接調(diào)用mysql

vim /etc/profile 
# 在profile文件末尾增加兩行 
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH 
export PATH 
# 使PATH搜索路徑立即生效:
source /etc/profile

2、登錄

1)查看初始密碼
[root@VM mysql]# cat /data/mysql/mysql-error.log | grep password 
2018-04-10T15:54:18.596286Z 1 [Note] A temporary password is generated for root@localhost: +uAqjuq/g7hv
2)登錄成功
[root@VM_38_201_centos mysql]# mysql -uroot -p+uAqjuq/g7hv
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 6
Server version: 5.7.21
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> 
3)修改登錄密碼
mysql> set password=password('123654');
Query OK, 0 rows affected, 1 warning (0.00 sec)



參考文檔:
https://blog.csdn.net/langzi7758521/article/details/51435985
https://blog.csdn.net/u013887008/article/details/79115183
http://www.cnblogs.com/galengao/p/5755788.html
https://blog.csdn.net/xyang81/article/details/51792144
https://www.cnblogs.com/xiangshui/p/5194990.html
https://blog.csdn.net/fxnawm/article/details/78497314
https://www.cnblogs.com/shengdimaya/p/8027507.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容