mysql 5.7 编译安装脚本。

时间:2023-01-28 07:30:15

此脚本尽量运行在centos 服务器上面,用于编译安装mysql 5.7

将此脚本和相应的软件 都放到/usr/local/src 目录下面

由于不能上传附件  所以需要把cmake-3.9.6.tar.gz ,boost_1_59_0.tar.gz,mysql-5.7.22.tar.gz 先下载好。

#####编译安装mysql5.7
#####此脚本的作用在于自动化安装mysql5.7 为并行复制(基于组提交), mysql5.7 自动化failover ,和archer 工单审核做准备
#####version mysql5.7.22
####下载msyql https://downloads.mysql.com/archives/community/mysql-5.7.22.tar.gz
###先将脚本 压缩本上传到服务器/usr/local/src/目录下 这是我习惯用的统一目录

####安装必要的依赖包
yum -y install gcc gcc-c++ ncurses ncurses-devel bison libgcrypt perl make openssl-devel

####安装cmake 去这里面下载分为源码版和编译好的版本https://cmake.org/download/
####添加用户 创建目录
groupadd -r mysql && useradd -r -g mysql -s /sbin/nologin -M mysql
mkdir /data/mysql/3306/{data,log,tmp,mysql-bin,relay-bin,redo-log,undo-log} -p
chown mysql.mysql /data/mysql -R
chmod 755 /data/mysql -R
#安装cmake
cd /usr/local/src
tar -xvzf cmake-3.9.6.tar.gz
cd /usr/local/src/cmake-3.9.6/
./bootstrap
sleep 10
gmake
make install
cd ..
#安装boost mysql5.7依赖
tar -xvzf boost_1_59_0.tar.gz -C /data/mysql/
######安装mysql5.7
tar -xvzf mysql-5.7.22.tar.gz
cd mysql-5.7.22
#指定boost的位置 -DWITH_BOOST=/data/mysql/boost_1_59_0
/usr/local/src/cmake-3.9.6/bin/cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/3306/data -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/data/mysql/boost_1_59_0 -DSYSCONFDIR=/etc/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DENABLE_DTRACE=0 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_EMBEDDED_SERVER=1 -DMYSQL_TCP_PORT=3306;

make -j `grep processor /proc/cpuinfo | wc -l` #编译时会消耗很大内存,小内存可能无法编译完成
make install
cd /usr/local/src/
rm -rf *.tar.gz
rm -rf mysql-5.7.22
####添加环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin">>/etc/profile
source /etc/profile

cat > /usr/local/mysql/my.cnf <<"EOF"
[client]

port = 3306
socket = /data/mysql/3306/tmp/mysql.socket

[mysqld]

server-id = 100
port = 3306
basedir=/usr/local/mysql
datadir=/data/mysql/3306/data
tmpdir = /tmp
socket = /data/mysql/3306/tmp/mysql.socket
skip-external-locking
skip_name_resolve = 1
transaction_isolation = REPEATABLE-READ
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
lower_case_table_names = 1
max_connections = 8000
max_connect_errors = 8000
explicit_defaults_for_timestamp = 1
max_allowed_packet = 256M
interactive_timeout = 1800
wait_timeout = 1800
tmp_table_size = 128M
max_heap_table_size = 128M
query_cache_size = 0
query_cache_type = 0
read_buffer_size = 1M
read_rnd_buffer_size = 2M
secure-file-priv = "/"
binlog_cache_size = 8M
join_buffer_size = 128M
back_log = 600
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
#######log set#############
log_error = /data/mysql/3306/log/mysql.err
slow_query_log = 1
slow_query_log_file = /data/mysql/3306/log/slow.log
log_queries_not_using_indexes = 1
long_query_time = 2
expire_logs_days = 7
log-bin=/data/mysql/3306/mysql-bin/mysql-bin
binlog_format = ROW
binlog_row_image = minimal
relay_log = /data/mysql/3306/relay-bin/relay-bin
####用户从库级联复制
log_slave_updates
####relay_log_recovery = 1 ##从库参数
slave_skip_errors = 1062
######刷盘方式#########
innodb_flush_log_at_trx_commit = 2
sync_binlog = 0

#########Innodb设置##########

innodb_buffer_pool_size = 512M
innodb_buffer_pool_instances = 8
#innodb_log_buffer_size = 16777216
innodb_flush_method = O_DIRECT
innodb_write_io_threads = 8

innodb_read_io_threads = 8
innodb_lock_wait_timeout = 60
innodb_log_group_home_dir = /data/mysql/3306/redo-log/
innodb_undo_directory = /data/mysql/3306/undo-log/
innodb_log_files_in_group = 3
innodb_log_file_size = 1G #线上改成4G

innodb_undo_log_truncate = 1

#innodb_undo_tablespaces = 1#线上改成3

innodb_undo_logs = 128
innodb_thread_concurrency = 64
innodb_print_all_deadlocks = 1
######gtid#######
gtid_mode = on
enforce_gtid_consistency = 1

######并行复制参数从库需要开启###########
#slave-parallel-type=LOGICAL_CLOCK
#slave-parallel-workers=16
master_info_repository=TABLE
relay_log_info_repository=TABLE
#relay_log_recovery=ON
replicate-wild-ignore-table = mysql.%
replicate-wild-ignore-table = test.%
[myisamchk]

key_buffer_size = 20M
sort_buffer_size = 4M

read_buffer = 2M

write_buffer = 2M

[mysqld_safe]

#增加每个进程的可打开文件数量.

open-files-limit = 28192
[mysqld-5.7]
innodb_buffer_pool_dump_pct = 40
innodb_page_cleaners = 4
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 2G
innodb_purge_rseg_truncate_frequency = 128
binlog_gtid_simple_recovery=1
log_timestamps=system
transaction_write_set_extraction=MURMUR32
show_compatibility_56=on
EOF
touch /data/mysql/3306/log/mysql.err
chown mysql.mysql /data/mysql/3306/log/mysql.err
####初始化
/usr/local/mysql/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql --datadir=/data/mysql/3306/data/ --explicit_defaults_for_timestamp
#####启动mysql
/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf &

sleep 300

######修改数据库密码

#/usr/local/mysql/bin/mysqladmin -u root -p -S /data/mysql/3306/tmp/mysql.socket password "abc123"
/usr/local/mysql/bin/mysql --port=3306 -S /data/mysql/3306/tmp/mysql.socket -BNe "update mysql.user set authentication_string=password('abc123') where user='root';flush privileges;"
####配置快捷登陆方式

cat >/usr/local/local.cnf <<EOF
[client]
host=localhost
port=3306
user=root
password=abc123
socket=/data/mysql/3306/tmp/mysql.socket
EOF

cat >>/root/.bashrc <<EOF
alias mysql.start='/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf &'
alias mysql.login='/usr/local/mysql/bin/mysql --defaults-file=/usr/local/local.cnf'
alias mysql.stop='/usr/local/mysql/bin/mysqladmin --port=3306 -uroot -p'abc123' -S /data/mysql/3306/tmp/mysql.socket shutdown'
EOF

source /root/.bashrc
source /root/.bash_profile
cat >>/etc/rc.local <<EOF
alias mysql.3310.start='/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf &'
alias mysql.3310.login='/usr/local/mysql/bin/mysql --defaults-file=/usr/local/local.cnf'
alias mysql.3310.stop='/usr/local/mysql/bin/mysqladmin --port=3310 -uroot -p'Pass@word' -S /data/mysql/3310/tmp/mysql.socket shutdown'
EOF
source /root/.bashrc
echo "Please execute:
source /root/.bash_profile"