MySQL编译步骤(Linux系统)

本文详述了在Linux环境下编译安装MySQL5.7的全过程,包括下载源码包、配置参数、创建用户组及用户、配置环境变量、安装服务及开机自启动等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MySQL编译过程(亲测)

MySQL编译步骤(Linux系统)

编译详细步骤

本文参考github上优秀博主的分享
https://github.com/Hackeruncle/MySQL/blob/master/MySQL%205.7.11%20Install.txt

一. 下载源代码包(注意不是已编译好的包,如rpm

  1. 链接:https://dev.mysql.com/downloads/mysql/
    在这里插入图片描述
    注:如果想编译图片中8.0.18版本亦可,本文以5.7.28版本为例
  2. 点击 Looking for previous GA versions?即可查看不同版本,如下图选择,po 主使用的是Centos7.x系列,所以选择下面的版本。
    在这里插入图片描述
  3. 往下拉会看到具体的版本和对应的包类型,包含 rpm,tar 包等。这里选择 Compressed Tar Archive,即源代码包。
    在这里插入图片描述

二. 源代码管理

  1. 将下载好的源代码包上传到虚拟机内,可通过 x-shell(Linux) 或者文件共享( Mac ),本文是在 Mac 上操作的( Mac上安装虚拟机,并用 Vmware tools 文件共享完成,Mac 安装虚拟机具体步骤待后续更新,亲测Mac虚拟机使用起来还挺顺手)
  2. 文件上传后,放到虚拟机 /usr/local 目录下,并解压(命令:tar zxvf mysql-5.7.28-el7-x86_64.tar.gz )
  3. 重命名解压后文件:mv mysql-5.7.28-el7-x86_64 mysql (个人习惯,也可不重命名)

三. 编译

1. 配置参数
  1. 先在 /usr/local 下创建目录 data arch 和 tmp,cd /usr/local mkdir data arch tmp,后续配置 my.cnf 文件时,需要用到这三个目录。
  2. 配置 my.cnf 文件。
  3. 以下附上所有可能用到的配置信息,具体每个配置信息的含义在其上面的注释,且下方的配置信息可直接复制粘贴使用(亲测可用,只需根据机器配置不同,稍作修改)。
[client]
# 客户端连接的端口
port = 3306
# mysql.sock的作用是server和client在同一台服务器,并且使用localhost进行链接的时候,就会使用socket来进行连接
socket = /usr/local/mysql/data/mysql.sock
# 默认编码格式
default-character-set=utf8mb4

[mysqld]
# mysqld其实是SQL后台程序(也就是MySQL服务器),它是关于服务器端的一个程序,mysqld意思是mysql daemon,在后台运行,监听3306端口
port = 3306
socket = /usr/local/mysql/data/mysql.sock
skip-slave-start
skip-external-locking
key_buffer_size = 256M
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 4M
query_cache_size= 32M
max_allowed_packet = 16M
myisam_sort_buffer_size=128M
tmp_table_size=32M
table_open_cache = 512
thread_cache_size = 8
wait_timeout = 86400
interactive_timeout = 86400
max_connections = 600
# Try number of CPU's*2 for thread_concurrency
#thread_concurrency = 32
#isolation level and default engine
default-storage-engine = INNODB
transaction-isolation = READ-COMMITTED
# server-id = 1 #表示是本机的序号为1,一般来讲就是master的意思
server-id = 1
basedir = /usr/local/mysql
# 数据目录
datadir = /usr/local/mysql/data
# 与数据相关的日志目录
pid-file = /usr/local/mysql/data/hostname.pid
# open performance schema
log-warnings
sysdate-is-now
# 做主从复制时用到的参数,作为从节点复制主节点数据的配置项
binlog_format = ROW
log_bin_trust_function_creators=1
log-error = /usr/local/mysql/data/hostname.err
log-bin = /usr/local/mysql/arch/mysql-bin
# 超过7天的binlog删除
expire_logs_days = 7
innodb_write_io_threads=16
# 读取bin-log来功放,相当于中继器作用
relay-log = /usr/local/mysql/relay_log/relay-log
relay-log-index = /usr/local/mysql/relay_log/relay-log.index
relay_log_info_file= /usr/local/mysql/relay_log/relay-log.info
log_slave_updates=1
gtid_mode=OFF
enforce_gtid_consistency=OFF
# slave
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=4
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
#other logs
#general_log =1
#general_log_file = /usr/local/mysql/data/general_log.err
#slow_query_log=1
#slow_query_log_file=/usr/local/mysql/data/slow_log.err
#for replication slave
sync_binlog = 500

# for innodb options
innodb_data_home_dir = /usr/local/mysql/data/
innodb_data_file_path = ibdata1:1G;ibdata2:1G:autoextend
# 此参数确定数据日志文件的大小,更大的设置可以提高性能,但也会增加恢复故障数据库所需的时间,需要视机器性能和存储空间调整
innodb_log_group_home_dir = /usr/local/mysql/arch
innodb_log_files_in_group = 4
innodb_log_file_size = 200M
innodb_log_buffer_size = 200M
# 根据生产需要,pool size需要调整大小
# 不要设置过大,否则,由于物理内存的竞争可能导致操作系统的换页颠簸.
# 注意在32位系统上你每个进程可能被限制在 2-3.5G 用户层面内存限制,
# 所以不要设置的太高.
innodb_buffer_pool_size = 200M
# innodb_additional_mem_pool_size = 50M
tmpdir = /usr/local/mysql/tmp
innodb_lock_wait_timeout = 1000
# innodb_thread_concurrency = 0
innodb_flush_log_at_trx_commit = 2
innodb_locks_unsafe_for_binlog=1
#innodb io features: add for mysql5.5.8
performance_schema
innodb_read_io_threads=4
innodb-write-io-threads=4
innodb-io-capacity=200
#purge threads change default(0) to 1 for purge
innodb_purge_threads=1
innodb_use_native_aio=on
#case-sensitive file names and separate tablespace
innodb_file_per_table = 1
lower_case_table_names=1
[mysqldump]
quick
# 服务器发送和接受的最大包长度
max_allowed_packet = 128M 
[mysql]
no-auto-rehash
default-character-set=utf8mb4
[mysqlhotcopy]
interactive-timeout
[myisamchk]
key_buffer_size = 50M
sort_buffer_size = 50M
read_buffer = 2M
write_buffer = 2M

2. 开始编译

  1. 创建用户组及用户
    [root@hadoop01 mysql]# groupadd -g 101 dab
    [root@hadoop01 mysql]# groupdel dab
    [root@hadoop01 mysql]# groupadd -g 101 dba
    [root@hadoop01 mysql]# useradd -u 514 -g dba
    [root@hadoop01 mysql]# usermod -u 514 -g dba -G root -d /usr/local/mysql mysqladmin

  2. 将环境变量配置文件 copy 至 mysqladmin 用户的 home 目录中,为了下一步配置个人环境变量
    [root@hadoop01 mysql]# cp /etc/skel/.* /usr/local/mysql
    cp: 略过目录"/etc/skel/."
    cp: 略过目录"/etc/skel/…"

  3. 配置环境变量
    [root@hadoop01 local]# vi mysql/.bash_profile

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# 用户指定的mysql目录,需要根据自己的来设定
export MYSQL_BASE=/usr/local/mysql
export PATH=${MYSQL_BASE}/bin:$PATH

unset USERNAME
# stty erase ^H
set umask to 022
umask 022
PS1=`uname -n`":"'$USER'":"'$PWD'":>"; export PS1
# end
  1. 赋权限和用户组,切换用户mysqladmin,安装
    [root@hadoop01 local]# chown mysqladmin:dba /etc/my.cnf
    [root@hadoop01 local]# chmod 640 /etc/my.cnf
    [root@hadoop01 local]# chown -R mysqladmin:dba /usr/local/mysql
    [root@hadoop01 local]# chmod -R 755 /usr/local/mysql

  2. 配置服务及开机自启动
    [root@hadoop01 local]# cd /usr/local/mysql/

#将服务文件拷贝到init.d下,并重命名为mysql
[root@hadoop01 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysql

#赋予可执行权限
[root@hadoop01 mysql]# chmod +x /etc/rc.d/init.d/mysql

#删除服务
[root@hadoop01 mysql]# chkconfig --del mysql

#添加服务
[root@hadoop01 mysql]# chkconfig --add mysql
[root@hadoop01 mysql]# chkconfig --level 345 mysql on

  1. 安装libaio及安装mysql的初始db
    [root@hadoop01 mysql]# yum -y install libaio
    [root@hadoop01 mysql]# sudo su - mysqladmin
    hadoop01:mysqladmin:/usr/local/mysql:>
    注:shell中多行执行,需要用反斜杠分割
> bin/mysqld \
> --defaults-file=/etc/my.cnf \
> --user=mysqladmin \
> --basedir=/usr/local/mysql/ \
> --datadir=/usr/local/mysql/data/ \
> --initialize

在初始化时如果加上 –initial-insecure,则会创建空密码的 root@localhost 账号,否则会创建带密码的 root@localhost 账号,密码直接写在 log-error 日志文件中(在5.6版本中是放在 ~/.mysql_secret 文件里,更加隐蔽,不熟悉的话可能会无所适从)
注:执行上述命令后,会刷出一些日志信息,等一会就会结束

hadoop01:mysqladmin:/usr/local/mysql/data:>2019-12-14T09:41:58.323350Z mysqld_safe Logging to ‘/usr/local/mysql/data/hostname.err’.
2019-12-14T09:41:58.347191Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
。。。

  1. 查看临时密码(随机生成)
    hadoop01:mysqladmin:/usr/local/mysql:>cd data/
    hadoop01:mysqladmin:/usr/local/mysql/data:>cat hostname.err | grep password
    2019-12-14T09:40:35.309762Z 1 [Note] A temporary password is generated for root@localhost: (FSj=:myr6a1

  2. 启动
    hadoop01:mysqladmin:/usr/local/mysql/data:>/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &

  3. 登录及修改用户密码
    hadoop01:mysqladmin:/usr/local/mysql:>mysql -u root -p
    Enter password: 输入上面生成的随机密码(FSj=:myr6a1)
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.28-log
    Copyright © 2000, 2019, 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> show databases; 第一次输入查看数据库会报错,因为必须要重设密码才可访问,所以才有下面的修改权限和密码的步骤
    ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
    mysql> alter user root@localhost identified by ‘mysql12345’
    -> ;
    Query OK, 0 rows affected (0.01 sec)
    mysql> grant all privileges on . to ‘root’@’%’ identified by ‘mysql12345’;
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    mysql> exit
    Bye

  4. 重启
    hadoop01:mysqladmin:/usr/local/mysql:>mysql -u root -p
    Enter password:
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.7.28-log MySQL Community Server (GPL)
    Copyright © 2000, 2019, 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> show databases;
    ±-------------------+
    | Database |
    ±-------------------+
    | information_schema |
    | mysql |
    | performance_schema |
    | sys |
    ±-------------------+
    4 rows in set (0.00 sec)

至此,mysql编译过程完成,即可对数据库进行创建数据库和表,增删改查操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangfeidianzi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值