一、安装准备
安装依赖包
# yum -y install gcc gcc-c++ ncurses ncurses-devel cmake bison
相关依赖包的作用:cmake:由于从 MySQL5.5 版本开始弃用了常规的 configure 编译方法,所以需要 CMake 编译器,用于设置 mysql 的编译参数。如:安装目录、数据存放目录、字符编码、排序规则等。
boost:从 MySQL 5.7.5 开始 Boost 库是必需的,mysql 源码中用到了 C++的 Boost 库,要求必须安装boost1.59.0 或以上版本
gcc:是 Linux 下的 C 语言编译工具,mysql 源码编译完全由 C 和 C++编写,要求必须安装GCC
bison:Linux 下 C/C++语法分析器
ncurses:字符终端处理库
二、下载相应源码包
# wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
# wget https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.9.tar.gz
三、新建MySQL用户和用户组
# groupadd -r mysql
# useradd -r -g mysql -s /sbin/nologin -M mysql
四、预编译
# tar -zxf boost_1_59_0.tar.gz
# tar -zxf mysql-5.7.11.tar.gz
# mkdir -p /data/mysql
# cd mysql-5.7.11
# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/tem/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_DEBUG=0 \
-DMYSQL_MAINTAINER_MODE=0 \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=../boost_1_59_0 \
-DWITH_SSL:STRING=bundled \
-DWITH_ZLIB:STRING=bundled
############################################
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ [MySQL安装的根目录]
-DMYSQL_DATADIR=/data/mysql \ [MySQL数据库文件存放目录]
-DSYSCONFDIR=/etc \ [MySQL配置文件所在目录]
-DMYSQL_USER=mysql \ [MySQL用户名]
-DWITH_MYISAM_STORAGE_ENGINE=1 \ [MySQL的数据库引擎]
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ [MySQL的数据库引擎]
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \ [MySQL的数据库引擎]
-DWITH_MEMORY_STORAGE_ENGINE=1 \ [MySQL的数据库引擎]
-DWITH_READLINE=1 \ [MySQL的readline library]
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock\ [MySQL的通讯目录]
-DMYSQL_TCP_PORT=3306 \ [MySQL的监听端口]
-DENABLED_LOCAL_INFILE=1 \ [启用加载本地数据]
-DENABLE_DOWNLOADS=1 \ [编译时允许自主下载相关文件]
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \ [使MySQL支持所有的扩展字符]
-DDEFAULT_CHARSET=utf8 \ [设置默认字符集为utf8]
-DDEFAULT_COLLATION=utf8_general_ci \ [设置默认字符校对]
-DWITH_DEBUG=0 \ [禁用调试模式]
-DMYSQL_MAINTAINER_MODE=0 \
-DDOWNLOAD_BOOST=1 \ [允许在线更新boost库]
-DWITH_BOOST=../boost_1_59_0 \ [指定boost安装路径]
-DWITH_SSL:STRING=bundled \ [通讯时支持ssl协议]
-DWITH_ZLIB:STRING=bundled [允许使用zlib library]
####################################################
五、编译安装
# make -j `grep processor /proc/cpuinfo | wc -l`
//编译很消耗系统资源,小内存可能编译通不过
# make install
// 花费时间会较长
如果中途编译失败了,需要删除cmake生成的预编译配置参数的缓存文件和make编译后生成的文件,再重新编译。
# make clean
# rm -rf CMakeCache.txt
六、更改目录权限
# chown -R mysql:mysql /usr/local/mysql
# chown -R mysql:mysq l/data/mysql
七、设置启动脚本,开机自启动
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# vim /etc/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
# chmod +x /etc/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
八,修改环境变量,添加库文件路径
# vim /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
# source /etc/profile
# vim /etc/ld.so.conf
/usr/local/mysql/lib
# ldconfig
九、自定义配置文件
/etc/my.cnf,仅供参考
cat > /etc/my.cnf << EOF
[client]
port = 3306
socket= /tmp/mysql.sock
default-character-set = utf8mb4
[mysqld]
port = 3306
socket= /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
pid-file= /data/mysql/mysql.pid
user= mysql
bind-address = 0.0.0.0
server-id= 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
#skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
EOF
十、初始化数据库
# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysq
注:
MySQL之前版本mysql_install_db是在mysql_basedir/script下
MySQL 5.7直接放在了mysql_install_db/bin目录下。
"–initialize"已废弃,生成一个随机密码(~/.mysql_secret)
"--initialize-insecure "不会生成密码
"–datadir"目录下不能有数据文件
十一、启动数据库
# systemctl start mysqld
# systemctl status mysqld
# systemctl enable mysqld
十二、查看MySQL服务进程和端口
# netstat -pantu | grep 3306
# ps -elf | grep mysqld
十三、设置数据库root用户密码
MySQL和Oracle数据库一样,数据库也默认自带了一个 root 用户(这个和当前Linux主机上的root用户是完全不搭边的),我们在设置好MySQL数据库的安全配置后初始化root用户的密码。配制过程中,一路输入 y 就行了。这里只说明下MySQL5.7版本中,用户密码策略分成低级 LOW 、中等 MEDIUM 和超强 STRONG 三种,推荐使用中等 MEDIUM 级别!
# mysql_secure_installation
十四、登录数据路
# mysql -u root -h localhost -p
十五、selinux 防火请设置
selinux:
# sed-i"s/SELINUX=enforcing/SELINUX=disabled/"/etc/selinux/config
#setenforce 0
iptables:
# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
# iptables-save
firewalld:
# firewall-cmd --zone=public --add-port=3306/tcp--permanent
# firewall-cmd --reload