源码编译安装MySQL5.7

1)下载软件包

[root@mysql ~]# wget -c https://downloads.mysql.com/archives/get/file/mysql-5.7.27.tar.gz
[root@mysql ~]#	wget -c https://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

2)安装相应的依赖包

[root@mysql ~]#	yum -y install ncurses ncurses-devel cmake libaio libaio-devel pcre pcre-devel \
zlib zlib-devel bison bison-devel libverto libverto-devel libstdc++ libstdc++-devel \
dbus dbus-devel libss libss-devel gcc gcc-c++ autoconf m4 libgcc e2fsprogs perl-Data-Dumper

3)为MySQL创建运行用户与组

[root@mysql ~]# groupadd mysql
[root@mysql ~]# useradd -M -s /sbin/nologin mysql -g mysql

4)解压boost工具、并移动到指定位置即可、无需编译安装

[root@mysql ~]# tar xf boost_1_59_0.tar.gz 
[root@mysql ~]# mv boost_1_59_0 /usr/local/boost

5)编译安装MySQL

[root@mysql ~]# tar xf mysql-5.7.27.tar.gz -C /usr/src/
[root@mysql ~]# cd /usr/src/mysql-5.7.27/
[root@mysql mysql-5.7.27]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_DEBUG=0 \
-DWITH_BOOST=/usr/local/boost \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock
make -j 8 && make install -j 8 && cd ~

6)编译安装完成以后、创建对应的目录、并调整相应的参数

[root@mysql ~]# mkdir /usr/local/mysql/pid
[root@mysql ~]# mkdir /usr/local/mysql/logs
[root@mysql ~]# mkdir /usr/local/mysql/socket
[root@mysql ~]# mkdir /usr/local/mysql/tmp
[root@mysql ~]# mkdir /usr/local/mysql/ibtmp
[root@mysql ~]# mkdir /usr/local/mysql/binlog
[root@mysql ~]# mkdir /usr/local/mysql/relaylog
[root@mysql ~]# mkdir /usr/local/mysql/outcsv/
[root@mysql ~]# mkdir /usr/local/mysql/ibdata
[root@mysql ~]# mkdir /usr/local/mysql/undolog
[root@mysql ~]# mkdir /usr/local/mysql/redolog
[root@mysql ~]# chown -R mysql:mysql /usr/local/mysql
[root@mysql ~]# chmod -R 750 /usr/local/mysql/outcsv
[root@mysql ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
[root@mysql ~]# source /etc/profile

7)定义MySQL配置文件

[root@mysql ~]#	cat > /etc/my.cnf << EOF
[client]
port = 3635
socket = /usr/local/mysql/socket/mysql.sock

[mysqld]
user = mysql
port = 3635
federated
skip_ssl
bind_address = 0.0.0.0
max_connections = 3600
max_connect_errors = 200
autocommit = ON
skip-name-resolve
symbolic-links = 0
skip-external-locking
log_timestamps = system
explicit_defaults_for_timestamp = ON
transaction_isolation = read-committed
binlog_gtid_simple_recovery = ON
show_compatibility_56 = ON
transaction_write_set_extraction = OFF
socket = /usr/local/mysql/socket/mysql.sock
pid-file = /usr/local/mysql/pid/mysql.pid
log-error = /usr/local/mysql/logs/mysql_error.log
secure-file-priv = /usr/local/mysql/outcsv
innodb_tmpdir = /usr/local/mysql/ibtmp
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
tmpdir = /usr/local/mysql/tmp

character-set-server = utf8
init_connect = SET NAMES utf8
collation-server = utf8_general_ci

slow_query_log = ON
long_query_time = 1
min_examined_row_limit = 960
log_slow_admin_statements = ON
log_slow_slave_statements = ON
log_queries_not_using_indexes = OFF
slow_query_log_file = /usr/local/mysql/logs/mysql_slow.log

back_log = 360
tmp_table_size = 64M
max_allowed_packet = 64M
max_heap_table_size = 64M
sort_buffer_size = 1M
join_buffer_size = 1M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
thread_cache_size = 64
thread_stack = 256K
query_cache_size = 32M
query_cache_limit = 2M
query_cache_min_res_unit = 2K
table_open_cache = 4096
open_files_limit = 65535
connect_timeout = 9
interactive_timeout = 21600
wait_timeout = 21600

innodb_data_file_path = ibdata1:12M;ibdata:12M:autoextend
innodb_autoextend_increment = 12
innodb_data_home_dir = /usr/local/mysql/ibdata

innodb_undo_tablespaces = 4
innodb_undo_logs = 128
innodb_max_undo_log_size = 1G
innodb_undo_log_truncate = ON
innodb_purge_rseg_truncate_frequency = 10
innodb_undo_directory = /usr/local/mysql/undolog

innodb_log_file_size = 128M
innodb_log_buffer_size = 16M
innodb_log_files_in_group = 3
innodb_flush_log_at_trx_commit = 2
innodb_flush_log_at_timeout = 1
innodb_flush_method = O_DIRECT
innodb_log_group_home_dir = /usr/local/mysql/redolog

innodb_temp_data_file_path = ibtmp1:12M:autoextend:max:5G
innodb_fast_shutdown = 0

default-storage-engine = InnoDB
innodb_buffer_pool_size = 2G
table_open_cache_instances = 8
innodb_buffer_pool_chunk_size = 256MB
innodb_page_size = 16k
innodb_sort_buffer_size = 1MB
innodb_file_per_table = ON
innodb_large_prefix = ON
innodb_purge_threads = 8
innodb_page_cleaners = 8
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_neighbors = 0
innodb_lru_scan_depth = 1024
innodb_lock_wait_timeout = 60
innodb_print_all_deadlocks = ON
innodb_deadlock_detect = ON
innodb_strict_mode = ON
innodb_buffer_pool_load_at_startup = ON
innodb_buffer_pool_dump_at_shutdown = ON
EOF

8)对MySQL进行初始化

[root@mysql ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@mysql ~]# echo $?
0

9)将MySQL添加为系统服务

[root@mysql ~]# cp /usr/local/mysql/support-files/mysql.server /usr/local/mysql/bin/mysql.sh
[root@mysql ~]# chmod +x /usr/local/mysql/bin/mysql.sh
[root@mysql ~]#	cat > /usr/lib/systemd/system/mysql.service << EOF
[Unit]
Description=MySQL
After=network.target

[Service]
User=mysql
Group=mysql
Type=forking
PrivateTmp=false
LimitNOFILE=65535
ExecStart=/usr/local/mysql/bin/mysql.sh start
ExecStop=/usr/local/mysql/bin/mysql.sh stop

[Install]
WantedBy=multi-user.target
EOF

10)启动MySQL并为root用户设置密码

[root@mysql ~]# systemctl start mysql
[root@mysql ~]# systemctl enable mysql
[root@mysql ~]# netstat -anput | grep mysql
tcp        0      0 0.0.0.0:3635            0.0.0.0:*               LISTEN      34411/mysqld
[root@mysql ~]# mysql -e"update mysql.user set authentication_string=password('abc-123') where user='root';flush privileges;"

【只是为了打发时间】  

你可能感兴趣的