#查看系統版本
[root@ctos3 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core)
#下載源碼包,需要注意的是mysql5.7 編譯安裝需要boost 庫,可以在官網下載含boost的源碼包
#提示,下載的時候如果不想進行登錄或注冊,就點不用,開始下載
#開始源碼編譯安裝
#1.安裝相關依賴包
yum install -y gcc gcc-c++ cmake ncurses ncurses-devel bison wget openssl-devel.x86_64
#2.創建用戶和組
groupadd mysql
useradd mysql -s /sbin/nologin -M -g mysql
#3.下載mysql和解壓,也可以下載好使用rz(包名lrzsz)上傳
mkdir /home/demo/tools/ cd /home/demo/tools/
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.29.tar.gz
tar xf mysql-boost-5.7.29.tar.gz
#4.配置相關參數
[root@ctos3 tools]# cd mysql-5.7.29/ [root@ctos3 mysql-5.7.29]# pwd /home/demo/tools/mysql-5.7.29 [root@ctos3 mysql-5.7.29]# cmake -DCMAKE_INSTALL_PREFIX=/application/mysql -DMYSQL_DATADIR=/application/mysql/data -DMYSQL_UNIX_ADDR=/application/mysql/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost
#參數介紹
DCMAKE_INSTALL_PREFIX #指定MySQL程序的安裝目錄
DMYSQL_DATADIR #數據文件目錄
DMYSQL_UNIX_ADDR #socket文件路徑
DDEFAULT_CHARSET #指定服務器默認字符集,默認latin1
DDEFAULT_COLLATION #指定服務器默認的校對規則,默認latin1_general_ci
#5.編譯安裝
[root@ctos3 mysql-5.7.29]# make -j 2 && make install
#-j參數作用:編譯時會占用大量的系統資源,可以通過-j參數指定多個編譯命令進行並行編譯來提高速度,使用以下命令查看系統CPU核數
[root@ctos3 ~]# cat /proc/cpuinfo | grep processor |wc -l 2
#6.創建數據目錄和修改權限
[root@ctos3 mysql-5.7.29]# mkdir /application/mysql/data #存放數據目錄
[root@ctos3 mysql-5.7.29]# mkdir /application/mysql/tmp #存放sock目錄
[root@ctos3 mysql-5.7.29]# chown -R mysql.mysql /application/mysql/
#7.配置/etc/my.cnf文件
#注意:5.7版本沒有模板文件/application/mysql/support-files/my-default.cnf,可根據需要自行添加
[root@ctos3 ~]# cat /etc/my.cnf [mysqld] port = 3306 socket = /application/mysql/tmp/mysql.sock user = mysql basedir = /application/mysql datadir = /application/mysql/data pid-file = /application/mysql/data/mysql.pid sql_mode='ONLY_FULL_GROUP_BY' log_error = /application/mysql/mysql-error.log !includedir /etc/my.cnf.d [client] port = 3306 socket = /application/mysql/tmp/mysql.sock
#8.初始化數據庫
[root@ctos3 support-files]# /application/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
#9.生成服務啟動腳本
#設置環境變量 [root@ctos3 support-files]# echo 'export PATH=/application/mysql/bin:$PATH' >> /etc/profile [root@ctos3 support-files]# source /etc/profile [root@ctos3 support-files]# tail -1 /etc/profile export PATH=/application/mysql/bin:$PATH #生成啟動腳本 [root@ctos3 ~]# cd /application/mysql/support-files/ [root@ctos3 support-files]# ls magic mysqld_multi.server mysql-log-rotate mysql.server [root@ctos3 support-files]# cp mysql.server /etc/init.d/mysqld #啟動服務和設置開機自啟 [root@ctos3 support-files]# /etc/init.d/mysqld start Starting MySQL. SUCCESS!
[root@ctos3 support-files]# chkconfig --add mysqld [root@ctos3 support-files]# chkconfig mysqld on
#10.登錄MySQL(沒有密碼登錄)
[root@ctos3 ~]# mysql -uroot -p
Enter password:
#11.查看版本
mysql> select version(); +-----------+ | version() | +-----------+ | 5.7.29 | +-----------+ 1 row in set (0.00 sec)
#12.設置密碼
mysql> update mysql.user set authentication_string=password('guoke123') where user='root'; Query OK, 1 row affected, 1 warning (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)