centos7系統下編譯安裝MYSQL5.6教程

1、MYSQL5.6資料庫安裝

獲取mysql5.6軟件包

wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.35.tar.gz

解壓tar包

tar zxvf mysql-5.6.35.tar.gz

安裝mysql依賴包

yum list|grep ncurses-devel
yum -y install make gcc-c++ cmake bison-devel ncurses-devel

創建mysql系統組和系統用戶

groupadd mysql
useradd mysql -g mysql -M -s /sbin/nologin

創建mysql程式目錄和數據目錄

#作為mysql程式目錄
mkdir -p /data/application/mysql 
#作為mysql數據目錄
mkdir -p /data/application/mysqlData
#修改mysql數據目錄的執行用戶(這很重要,否則初始化資料庫可能無法成功)
chown -R mysql:mysql /data/application/mysqlData 

準備編譯 使用cmake指定mysql的編譯條件

cd mysql-5.6.23/ #進入到mysql目錄                                                  
cmake  -DCMAKE_INSTALL_PREFIX=/data/application/mysql \
-DMYSQL_DATADIR=/data/applicattion/mysqlData \
-DMYSQL_USER=mysql

編譯安裝make &&make install

make && make install #這個過程大概要30分鐘

如果不出意外,安裝完畢之後可以在/data/application/mysql目錄下看到mysql的相關文件

drwxr-xr-x 13 root root  4096 Aug 27 14:29 .
drwxr-xr-x  6 root root  4096 Aug 27 14:30 ..
drwxr-xr-x  2 root root  4096 Aug 27 13:50 bin
-rw-r--r--  1 root root 17987 Nov 28  2016 COPYING
drwxr-xr-x  3 root root  4096 Aug 27 13:50 data
drwxr-xr-x  2 root root  4096 Aug 27 13:50 docs
drwxr-xr-x  3 root root  4096 Aug 27 13:50 include
drwxr-xr-x  3 root root  4096 Aug 27 13:50 lib
drwxr-xr-x  4 root root  4096 Aug 27 13:50 man
drwxr-xr-x 10 root root  4096 Aug 27 13:50 mysql-test
-rw-r--r--  1 root root  2496 Nov 28  2016 README
drwxr-xr-x  2 root root  4096 Aug 27 13:50 scripts
drwxr-xr-x 28 root root  4096 Aug 27 13:50 share
drwxr-xr-x  4 root root  4096 Aug 27 13:50 sql-bench
drwxr-xr-x  2 root root  4096 Aug 27 13:50 support-files

準備啟動mysql, 初始化資料庫

cd /data/application/mysql/scripts
./mysql_install_db --basedir=/data/application/mysql --datadir=/data/application/mysqlData --user=mysql

查看/data/application/mysqlData目錄,會發現有以下目錄結構

drwxr-xr-x 5 mysql mysql     4096 Aug 27 14:45 .
drwxr-xr-x 6 root  root      4096 Aug 27 14:30 ..
-rw-rw---- 1 mysql mysql 12582912 Aug 27 14:45 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Aug 27 14:45 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Aug 27 14:30 ib_logfile1
drwx------ 2 mysql mysql     4096 Aug 27 14:30 mysql
drwx------ 2 mysql mysql     4096 Aug 27 14:30 performance_schema
drwx------ 2 mysql mysql     4096 Aug 27 14:30 test

安全啟動mysql 需要指定參數

cd /data/application/mysql/bin
./mysqld_safe --defaults-file=/data/application/mysql/my.cnf --basedir=/data/application/mysql --datadir=/data/application/mysqlData --user=mysql &

查看mysql是否啟動

root      6544 12272  0 14:45 pts/1    00:00:00 /bin/sh ./mysqld_safe --defaults-file=/data/application/mysql/my.cnf --basedir=/data/application/mysql --datadir=/data/application/mysqlData --user=mysql
mysql     6656  6544  0 14:45 pts/1    00:00:00 /data/application/mysql/bin/mysqld --defaults-file=/data/application/mysql/my.cnf --basedir=/data/application/mysql --datadir=/data/application/mysqlData --plugin-dir=/data/application/mysql/lib/plugin --user=mysql --log-error=/data/application/mysqlData/VM_140_205_centos.err --pid-file=/data/application/mysqlData/VM_140_205_centos.pid

修改資料庫賬號密碼

./mysqladmin -u root password #接著輸入兩次密碼即可

mysql客戶端連接

./mysql -uroot -p #輸入密碼
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.35 Source distribution
Copyright (c) 2000, 2016, 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.

2、MYSQL5.6系統設置
將mysql常用工具置為全局可用(建立軟連接)

ln -s /data/application/mysql/bin/mysqladmin /usr/bin/                  
ln -s /data/application/mysql/bin/mysqld_safe /usr/bin/                     
ln -s /data/application/mysql/bin/mysql /usr/bin/   

編寫mysql啟動停止管理shell腳本(mysql內容如下)

#!/bin/sh
mysql_port=port
mysql_username="user"  
mysql_password="pass"
function_start_mysql(){  
   printf "Starting MySQL...\n"  
   /data/application/mysql/bin/mysqld_safe --defaults-file=/data/application/mysql/my.cnf --basedir=/data/application/mysql --datadir=/data/application/mysqlData --user=mysql & 
}
function_stop_mysql(){  
   printf "Stoping MySQL...\n"  
   /data/application/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} shutdown
}

function_restart_mysql(){  
   printf "Restarting MySQL...\n"  
   function_stop_mysql  
   sleep  5
   function_start_mysql  
}     
if [ "$1" = "start" ]; then  
function_start_mysql  
elif [ "$1" = "stop" ]; then  
function_stop_mysql 
elif [ "$1" = "restart" ]; then  
function_restart_mysql   
else
printf "Usage: /data/mysql/${mysql_port}/mysql  {start|stop|restart|kill}\n"  
fi      

設置mysql開機重啟(mysqlAutoStart.sh內容如下)

#!/bin/sh
/data/application/mysqlShell/mysql start

將mysqlAutoStart.sh的所在目錄加到/etc/rc.d/rc.local文件末尾

#!/bin/sh
 /usr/sbin/ntpdate ntpupdate.tencentyun.com >/dev/null 2>&1 &
/usr/local/qcloud/rps/set_rps.sh >/tmp/setRps.log 2>&1
/usr/local/qcloud/irq/net_smp_affinity.sh >/tmp/net_affinity.log 2>&1
#secu_agent init monitor, install at Sat Aug 19 19:35:02 CST 2017
/usr/local/sa/agent/init_check.sh  > /dev/null 2>&1
/data/application/mysqlShell/mysqlAutoStart.sh #這便是mysql開機啟動設置

You May Also Like