在VMware已安装的CentOS7.9上编译安装mysql5.6

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用户远程登录等。

posted @ 2024-11-10 21:38  黄明文  阅读(6)  评论(0编辑  收藏  举报