在VMware已安裝的CentOS7.9上編譯安裝mysql5.6

黄明文發表於2024-11-10

VMware已安裝的CentOS7.9上編譯安裝mysql5.6

目錄

  • 1、安裝依賴
  • 2、下載MySQL原始碼
  • 3、解壓原始碼包
  • 4、編譯MySQL
    • 4.1、編譯前,cmake生成Makefile
    • 4.2、編譯MySQL
  • 5、安裝MySQL
  • 6、配置MySQL
    • 6.1、建立MySQL的使用者和組
    • 6.2、建立資料目錄並設定許可權
    • 6.3、初始化MySQL資料庫
  • 7、啟動MySQL
  • 8、安全配置

準備工作:VMware已安裝好CentOS7.9。VMware安裝CentOS7.9可以參考唐浩榮的部落格:在VMware中安裝CentOS7(超詳細的圖文教程)

本部落格使用到的映象站:阿里雲官方映象站https://developer.aliyun.com/mirror/

CentOS7.9.2009源:CentOS-7-x86_64-Everything-2009.iso

MySQL5.6.51源:mysql-5.6.51.tar.gz

為了後續的主從複製等操作,製作了VMware安裝的3個虛擬機器例項,虛擬機器設定記憶體大於等於4G,cpu至少2核以上。

| IP地址 | 虛擬機器名稱 | 賬號 |
| 192.168.58.159 | CentOS7_Master1 |root |
| 192.168.58.160 | CentOS7_Slave1 | root |
| 192.168.58.161 | CentOS7_Slave2 | root |

Selinux及防火牆的關閉,編譯安裝mysql5.6無需Selinux及防火牆關閉,對於初學者的我,不太會設定策略,所以先關閉,之後再開。

# 先檢視selinux的情況
getenforce

# 設定selinux為disabled
sudo vi /etc/selinux/config
SELINUX=disabled

# 重啟生效
reboot

# 重啟後再看狀態
getenforce

禁用前selinux情況

selinux禁用前配置:

selinux禁用後配置:

確認結果:

# 關閉防火牆
firewall-cmd --state
systemctl stop firewalld
systemctl disable firewalld
firewall-cmd --state

防火牆關閉前情況

防火牆關閉及關閉後的情況

回到頂部

1、安裝依賴

首先,配置下其他源:

CentOS 7的官方維護已於2024年6月30日正式結束,所以,生產環境需要使用其他作業系統,這裡用CentOS 7做實驗,要安裝依賴包,需要修改下相關的映象源。

配置方法見Alibaba,https://developer.aliyun.com/mirror/centos-vault

建議先備份 /etc/yum.repos.d/ 內的檔案。

需要確定您所需要的小版本,如無特殊需要則使用該大版本的最後一個小版本,比如 6.10,5.11,我們將其標記為 $minorver,需要您在之後的命令中替換。

然後編輯 /etc/yum.repos.d/ 中的相應檔案,在 mirrorlist= 開頭行前面加 # 註釋掉;並將 baseurl= 開頭行取消註釋(如果被註釋的話)。 對於 CentOS 8 之前的版本,請把該行內的域名及路徑(例如mirror.centos.org/centos/$releasever)替換為 mirrors.aliyun.com/centos-vault/$minorver。 對於 CentOS 8 ,請注意域名及路徑發生了更換,此時需要替換的欄位為 http://mirror.centos.org/$contentdir/$releasever

以上步驟可以被下方的命令完成

#  Centos 7
minorver=7.9.2009
sudo sed -e "s|^mirrorlist=|#mirrorlist=|g" \
         -e "s|^#baseurl=http://mirror.centos.org/centos/\$releasever|baseurl=https://mirrors.aliyun.com/centos-vault/$minorver|g" \
         -i.bak \
         /etc/yum.repos.d/CentOS-*.repo
                 
# CentOS 8 
minorver=8.5.2111
sudo sed -e "s|^mirrorlist=|#mirrorlist=|g" \
         -e "s|^#baseurl=http://mirror.centos.org/\$contentdir/\$releasever|baseurl=https://mirrors.aliyun.com/centos-vault/$minorver|g" \
         -i.bak \
         /etc/yum.repos.d/CentOS-*.repo

在編譯前,要安裝一些必要的依賴包:

# 安裝依賴包,視實際情況而定
sudo yum install -y gcc gcc-c++ ncurses-devel perl perl-devel readline-devel bison bison-devel bison-libs bison-doc libaio libaio-devel libselinux libselinux-devel libsepol libsepol-devel libselinux-python openldap openldap-devel numactl numactl-devel pam pam-devel openssl-devel wget
cmake net-tools -y

# 檢視已安裝依賴包的版本
rpm -p gcc gcc-c++ ncurses-devel perl perl-devel readline-devel bison bison-devel bison-libs bison-doc libaio libaio-devel libselinux libselinux-devel libsepol libsepol-devel libselinux-python openldap openldap-devel numactl numactl-devel pam pam-devel openssl-devel cmake wget cmake net-tools

# 結果
gcc-4.8.5-44.el7.x86_64
gcc-c++-4.8.5-44.el7.x86_64
ncurses-devel-5.9-14.20130511.el7_4.x86_64
perl-5.16.3-299.el7_9.x86_64
readline-devel-6.2-11.el7.x86_64
bison-3.0.4-2.el7.x86_64
bison-devel-3.0.4-2.el7.x86_64
package bison-libs is not installed
package bison-doc is not installed
libaio-0.3.109-13.el7.x86_64
libaio-devel-0.3.109-13.el7.x86_64
libselinux-2.5-15.el7.x86_64
libselinux-devel-2.5-15.el7.x86_64
libsepol-2.5-10.el7.x86_64
libsepol-devel-2.5-10.el7.x86_64
libselinux-python-2.5-15.el7.x86_64
openldap-2.4.44-25.el7_9.x86_64
openldap-devel-2.4.44-25.el7_9.x86_64
numactl-2.0.12-5.el7.x86_64
numactl-devel-2.0.12-5.el7.x86_64
pam-1.1.8-23.el7.x86_64
pam-devel-1.1.8-23.el7.x86_64
openssl-devel-1.0.2k-26.el7_9.x86_64
wget-1.14-18.el7_6.1.x86_64
cmake-2.8.12.2-2.el7.x86_64

2、下載MySQL原始碼

# 將所有外部工具都放在新的資料夾下面
mkdir -p /home/brian/tools/

cd /home/brian/tools/

# 下載MySQL原始碼到上述資料夾下,aliyun源下載的更快一些
wget https://mirrors.aliyun.com/mysql/MySQL-5.6/mysql-5.6.51.tar.gz
#wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.51.tar.gz
#wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.51.tar.gz

3. 解壓原始碼包

解壓下載的原始碼包:

tar -zxvf mysql-5.6.51.tar.gz
cd mysql-5.6.51

4. 編譯MySQL

在編譯之前,建議使用cmake來生成Makefile

  cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.51 \
  -DCMAKE_CXX_FLAGS="-std=c++11" \
  -DMYSQL_DATADIR=/application/mysql-5.6.51/data \
  -DMYSQL_UNIX_ADDR=/application/mysql-5.6.51/tmp/mysql.sock \
  -DDEFAULT_CHARSET=utf8 \
  -DDEFAULT_COLLATION=utf8_general_ci \
  -DWITH_EXTRA_CHARSETS=all \
  -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  -DWITH_FEDERATED_STORAGE_ENGINE=1 \
  -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
  -DWITH_ZLIB=bundled \
  -DWITH_SSL=system \
  -DENABLED_LOCAL_INFILE=1 \
  -DWITH_EMBEDDED_SERVER=1 \
  -DENABLE_DOWNLOADS=1 \
  -DWITH_DEBUG=0

要看到以下兩項都完成才行。

-- Configuring done
-- Generating done

如果有error,碰到什麼就處理什麼,第一步安裝依賴那麼多,就是為了避免在本環節出現因缺少依賴包導致的很多錯誤。

然後編譯MySQL:

make

make需要達到100%才行,需要花費些時間。

5. 安裝MySQL

編譯完成後,安裝MySQL:

sudo make install

cd /home/brian/tools/
ls /application/
cd /application/
# 驗證mysql安裝成功
# 建立軟連結
ln -s /application/mysql-5.6.51/ /application/mysql

# 建立連結後,您可以使用 ls -l 命令來檢查連結是否正確建立:
ls -l /application/mysql/

6. 配置MySQL

建立MySQL的使用者和組:

# 建立mysql使用者,用於授權目錄
useradd -s /sbin/nologin -M mysql

# 驗證是否建立了賬號
id mysql

建立資料目錄並設定許可權:

sudo mkdir -p /application/mysql/tmp
sudo chown -R mysql:mysql /application/mysql/

資料庫目前還無資料,需要做初始化

初始化MySQL資料庫:

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql

# 問題Data::Dumper未安裝
[root@centos7_master1 scripts]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql
FATAL ERROR: please install the following Perl modules before executing /application/mysql/scripts/mysql_install_db:
Data::Dumper

#出現上述問題,需安裝Dumper
sudo yum install -y perl perl-devel
sudo yum install -y perl-CPAN 
cpan Data::Dumper


# 安裝DUMPER過程中需要注意的
[root@centos7_slave2 scripts]# cpan Data::Dumper
Sorry, we have to rerun the configuration dialog for CPAN.pm due to
some missing parameters. Configuration will be written to
 <</root/.cpan/CPAN/MyConfig.pm>>


CPAN.pm requires configuration, but most of it can be done automatically.
If you answer 'no' below, you will enter an interactive dialog for each
configuration option instead.

Would you like to configure as much as possible automatically? [yes] yes

 <install_help>

Warning: You do not have write permission for Perl library directories.

To install modules, you need to configure a local Perl library directory or
escalate your privileges.  CPAN can help you by bootstrapping the local::lib
module or by configuring itself to use 'sudo' (if available).  You may also
resolve this problem manually if you need to customize your setup.

What approach do you want?  (Choose 'local::lib', 'sudo' or 'manual')
 [local::lib] local::lib
Would you like me to automatically choose some CPAN mirror
sites for you? (This means connecting to the Internet) [yes] yes
Would you like me to append that to /root/.bashrc now? [yes] yes

安裝Data:Dumper成功

初始化成功

7. 啟動MySQL

啟動MySQL服務:

# 使用MySQL自動化生成的管理指令碼
[root@centos7_master1 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@centos7_master1 mysql]# chmod 700 /etc/init.d/mysqld

# 提前準備好MySQL的my.conf
[root@centos7_master1 mysql]# cp support-files/my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? yes


# 啟動MySQL服務
[root@centos7_master1 mysql]# /etc/init.d/mysqld start

# 問題1:欠缺資料夾
Starting MySQL.Logging to '/application/mysql-5.6.51/data/centos7_master1.err'.
241111 03:23:42 mysqld_safe Directory '/application/mysql-5.6.51/tmp' for UNIX socket file don't exists.
 ERROR! The server quit without updating PID file (/application/mysql-5.6.51/data/centos7_master1.pid).
[root@centos7_master1 mysql]#

# 問題1解決:建立/application/mysql-5.6.51/tmp資料夾
[root@centos7_master1 mysql]# mkdir -p /application/mysql/tmp

# 問題2:檔案的許可權歸屬問題
[root@centos7_master1 mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/application/mysql-5.6.51/data/centos7_master1.err'.
.. ERROR! The server quit without updating PID file (/application/mysql-5.6.51/data/centos7_master1.pid).

# 問題2解決:檔案的許可權歸屬mysql賬號
[root@centos7_master1 mysql]# chown -R mysql.mysql /application/mysql/

# 成功啟動MySQL服務
[root@centos7_master1 mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@centos7_master1 mysql]# netstat -tunlp|grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      86789/mysqld

# 問題3:PATH變數還未修改
[root@centos7_master1 mysql]# mysql -uroot -p
-bash: mysql: command not found
[root@centos7_master1 mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

# 問題3解決
[root@centos7_master1 mysql]# vi /etc/profile
# 到最後加入下面語句
# 寫入PATH,加入MySQL的路徑
export PATH=/application/mysql/bin:$PATH

# source生效
[root@centos7_master1 mysql]#source /etc/profile

# 正常登入mysql
[root@centos7_master1 mysql]# echo $PATH
/root/perl5/bin:/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7_master1 mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.51 Source distribution

Copyright (c) 2000, 2021, 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出現的原因,檔案的許可權歸屬問題

問題2解決後:

8. 安全配置

執行安全指令碼:

[root@centos7_master1 scripts]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!




All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


Cleaning up...

按照提示設定root密碼,移除匿名使用者,禁止root使用者遠端登入等。

相關文章