CentOS7下MySQL5.7.20的安装-源码方式

初始化环境

cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 7.4 (Maipo)

# 配置主机名
hostnamectl --static set-hostname NAME

# 配置IP地址
vi /etc/sysconfig/network-scripts/ifcfg-ens160
#-----------------------------------------
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
NAME=ens160
UUID=67869621-df21-4ad4-9369-b58078ca74ea
DEVICE=ens160
ONBOOT=yes
IPADDR=10.240.4.179
PREFIX=24
GATEWAY=10.240.4.254
#-----------------------------------------

# 禁用firewall
systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld

# 禁用SELINUX
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

# 配置配认yum源为光盘
mkdir /media/disk
mount /dev/cdrom /media/disk
cd /etc/yum.repos.d/
mv CentOS-Base.repo CentOS-Base.repo_bak
vi CentOS-Base.repo
#-----------------------------------------
[centos7]
name=CentOS-$releasever
baseurl=file:///media/disk
gpgcheck=0
enabled=1
#-----------------------------------------

编译安装mysql

安装mysql源码编译依赖包及常用工具

yum -y install lrzsz
yum install cmake make gcc gcc-c++ ncurses ncurses-devel bison openssl-devel

# 删除原有mysql / mariadb相关包
rpm -qa | grep mysql
rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64

rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

添加用户组、用户和创建安装目录

groupadd mysql
useradd -g mysql mysql
echo "Mysql123456" | passwd --stdin mysql

# 当一台服务器部署多个mysql实例时,在相应的目录下创建名为[3306],[3307](端口号)等目录
# 这里为了标准化配置,单实例时也创建了名为[3306]的标准化目录
mkdir -p /usr/local/mysql
mkdir -p /u01/mysql/conf/3306
mkdir -p /u01/mysql/data/3306         
mkdir -p /u01/mysql/log/3306/iblog
mkdir -p /u01/mysql/log/3306/binlog
mkdir -p /u01/mysql/run/3306
mkdir -p /u01/mysql/tmp/3306

chown mysql.mysql -R /usr/local/mysql
chown mysql.mysql -R /u01/mysql
chmod -R 755 /u01/mysql

echo "export PATH=\$PATH:/usr/local/mysql/bin" >> /home/mysql/.bash_profile

MySQL运行环境配置

vi /etc/sysctl.conf
#-----------------------------------------
fs.file-max = 6815744
kernel.sem = 250 32000 100 128
kernel.shmmni = 4096
kernel.shmall = 4294967296
kernel.shmmax = 2199023255552
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.panic_on_oops = 1
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
fs.aio-max-nr = 1048576
net.ipv4.ip_local_port_range = 9000 65500
#-----------------------------------------

# kernel.shmmax算法:修改为物理内容的50%、60%
# 4G:kernel.shmmax = (4G*1024*1024*1024*1024)*50% = 2199023255552

# 没有设置的值可以通过以下方法查看,如
cat /proc/sys/net/ipv4/ip_forward
cat /proc/sys/net/ipv4/tcp_syncookies
cat /proc/sys/net/ipv4/conf/default/rp_filter
cat /proc/sys/net/ipv4/conf/default/accept_source_route

# 使配置立即生效
sysctl -p

vi /etc/security/limits.conf
#-----------------------------------------
mysql   soft   nofile    1024
mysql   hard   nofile    65536
mysql   soft   nproc     2047
mysql   hard   nproc     16384
mysql   soft   stack     10240
mysql   hard   stack     32768
#-----------------------------------------

编译安装mysql 5.7

# 上传boost_1_59_0.tar.gz mysql-5.7.20.tar.gz到服务器上
# wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
mkdir /usr/local/boost
cd /tmp
cp boost_1_59_0.tar.gz /usr/local/boost
tar xpvf mysql-5.7.20.tar.gz
cd mysql-5.7.20

# 开始编译
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DWITH_BOOST=/usr/local/boost \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_SSL=yes \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_DATADIR=/u01/mysql/data/3306 \
-DMYSQL_UNIX_ADDR=/u01/mysql/run/3306/mysql.sock \
-DSYSCONFDIR=/u01/mysql/conf/3306

## BLACKHOLE 黑洞引擎,写入的任何数据都会消失,用于记录binlog做复制的中继存储
## FEDERATED 通过这个引擎可以实现类似Oracle 下DBLINK的远程数据访问功能,
#            通过FEDERATED引擎创建的表只是在本地有表定义文件,数据文件则存在于远程数据库中

# 重新cmake需要删除当前目录下CMakeCache.txt,然后再重新执行

make && make install

配置my.cnf文件

su - mysql
cd /u01/mysql/conf/3306
vi my.cnf

[client]
port=3306
socket=/u01/mysql/run/3306/mysql.sock

[mysqld]
autocommit=0
general_log=off
explicit_defaults_for_timestamp=true
transaction_isolation=read-committed
lower_case_table_names=1

# system
basedir=/usr/local/mysql
datadir=/u01/mysql/data/3306
max_allowed_packet=1g
max_connections=3000
max_user_connections=2800
open_files_limit=65535
table_open_cache=2000
pid_file=/u01/mysql/run/3306/mysqld.pid
port=3306
server_id=1
skip_name_resolve=ON
socket=/u01/mysql/run/3306/mysql.sock
tmpdir=/u01/mysql/tmp/3306


#binlog
log_bin=/u01/mysql/log/3306/binlog/binlog
binlog_cache_size=32768
binlog_format=row
expire_logs_days=7
log_slave_updates=ON
max_binlog_cache_size=2147483648
max_binlog_size=524288000
sync_binlog=100

#logging
log_error=/u01/mysql/log/3306/error.log
slow_query_log_file=/u01/mysql/log/3306/slow.log
log_queries_not_using_indexes=0
slow_query_log=1
log_slave_updates=ON
log_slow_admin_statements=1
long_query_time=1

#relay
relay_log=/u01/mysql/log/3306/relaylog
relay_log_index=/u01/mysql/log/3306/relay.index
relay_log_info_file=/u01/mysql/log/3306/relay-log.info

#slave
slave_load_tmpdir=/u01/mysql/tmp/3306
slave_skip_errors=OFF

#innodb
innodb_data_home_dir=/u01/mysql/log/3306/iblog
innodb_log_group_home_dir=/u01/mysql/log/3306/iblog
innodb_adaptive_flushing=ON
innodb_adaptive_hash_index=ON
innodb_autoinc_lock_mode=1
innodb_buffer_pool_instances=8

#default
innodb_change_buffering=inserts
innodb_checksums=ON
innodb_buffer_pool_size= 128M
innodb_data_file_path=ibdata1:32M;ibdata2:16M:autoextend
innodb_doublewrite=ON
innodb_file_per_table=ON
innodb_flush_log_at_trx_commit=1
innodb_flush_method=O_DIRECT
innodb_io_capacity=1000
innodb_lock_wait_timeout=10
innodb_log_buffer_size=67108864
innodb_log_file_size=1048576000
innodb_log_files_in_group=4
innodb_max_dirty_pages_pct=60
innodb_open_files=60000
innodb_purge_threads=1
innodb_read_io_threads=4
innodb_stats_on_metadata=OFF
innodb_support_xa=ON
innodb_use_native_aio=OFF
innodb_write_io_threads=10

[mysqld_safe]
datadir=/u01/mysql/data/3306

初始化MySQL

# 使用以下命令可以看到mysql默认配置文件位置等信息
mysqld --verbose --help

cd /usr/local/mysql/bin
# 方法一
./mysqld --initialize-insecure --datadir=/u01/mysql/data/3306 --user=mysql
#初始化后,root密码为空


# 方法二
./mysqld --initialize --datadir=/u01/mysql/data/3306 --user=mysql
# 初始化后,root密码为一个临时密码,可以使用以下语句查看
grep 'temporary password' /u01/mysql/log/3306/error.log
2017-12-06T08:13:52.132699Z 1 [Note] A temporary password is generated for root@localhost: hZet!:+F1d=J

启动数据库

cd /usr/local/mysql/bin
mysqld_safe &

设置root密码

mysql_secure_installation

参考

文章目录
  1. 1. 初始化环境
  2. 2. 编译安装mysql
    1. 2.1. 安装mysql源码编译依赖包及常用工具
    2. 2.2. 添加用户组、用户和创建安装目录
    3. 2.3. MySQL运行环境配置
    4. 2.4. 编译安装mysql 5.7
    5. 2.5. 配置my.cnf文件
    6. 2.6. 初始化MySQL
    7. 2.7. 启动数据库
    8. 2.8. 设置root密码
  3. 3. 参考