centos7 编译安装mysql-5.7.18

卸载mariadb

# rpm -qa mariadb*
mariadb-libs-5.5.44-2.el7.centos.x86_64
# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

一、对于centos 7 安装mySql依赖项

yum install  gcc gcc-c++ make cmake bison bison-devel ncurses ncurses-devel libaio-devel perl autoconf automake zlib* fiex* libxml*  libmcrypt* libtool-ltdl-devel* 

二、创建用户

设置MySQL用户和组

  • 新增mysql用户组
  groupadd mysql 
  • 新增mysql用户
useradd -r -g mysql mysql 

三、新建MySQL所需要的目录

  • 新建mysql安装目录
mkdir -p /mysql/app  
  • 新建mysql数据库数据文件目录
mkdir -p /mysql/app/data
  • 新建mysql.sock数据库链接文件目录
mkdir -p /mysql/app/tmp
  • 新建mysql日志存放目录
mkdir -p /mysql/app/logs
  • 新建mysql 配置文件目录
mkdir -p  /mysql/app/etc

四、上传源码文件

  • mysql-5.7.18.tar.gz
  • 下载地址
  • http://120.198.244.60:9999/mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.18.tar.gz

解压编译

cmake -DCMAKE_INSTALL_PREFIX=/mysql/app -DMYSQL_DATADIR=/mysql/app/data -DSYSCONFDIR=/mysql/app/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/mysql/app/tmp/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost

参数解析:

  • CMAKE_INSTALL_PREFIX:指定MySQL程序的安装目录,默认/usr/local/mysql
  • DEFAULT_CHARSET:指定服务器默认字符集,默认latin1
  • DEFAULT_COLLATION:指定服务器默认的校对规则,默认latin1_general_ci
  • ENABLED_LOCAL_INFILE:指定是否允许本地执行LOAD DATA INFILE,默认OFF
  • WITH_COMMENT:指定编译备注信息
  • WITH_xxx_STORAGE_ENGINE:指定静态编译到mysql的存储引擎,MyISAM,MERGE,MEMBER以及CSV四种引擎默认即被编译至服务器,不需要特别指定。
  • WITHOUT_xxx_STORAGE_ENGINE:指定不编译的存储引擎
  • SYSCONFDIR:初始化参数文件目录
  • MYSQL_DATADIR:数据文件目录
  • MYSQL_TCP_PORT:服务端口号,默认3306
  • MYSQL_UNIX_ADDR:socket文件路径,默认/tmp/mysql.sock
  • DDOWNLOAD_BOOST=1 :下载boost库
  • DWITH_BOOST=/usr/local/boost 安装boost库

编译安装

make && make install
  • 中间大概2个小时
## 出错的情况下 需要重新编译才执行 否则不用操作 下面这两条命令
make clean
rm -f CMakeCache.txt

五、创建配置文件my.cnf

cp support-files/my-large.cnf /mysql/app/etc/my.cnf
  • 如果my-large.cnf 不存在,手动创建。
vi /mysql/app/etc/my.cnf
[client]
port = 3306
socket	= /mysql/tmp/mysqld.sock

[mysqld]
port = 3306
pid-file = /mysql/tmp/mysqld.pid
socket	= /mysql/tmp/mysqld.sock
datadir	= /mysql/data
log_error = /mysql/logs/mysql-error.log

event_scheduler = 1 
log_bin_trust_function_creators = 1
group_concat_max_len = 102400

slow_query_log = 1
long_query_time = 3 
slow_query_log_file = /mysql/logs/mysql-slow.log

#symbolic-links=0 #分区

user = mysql
bind-address = 0.0.0.0
character_set_server=utf8
init_connect='SET NAMES utf8'

server-id = 1

wait_timeout=31536000
interactive_timeout=31536000
skip-name-resolve  
back_log = 600  
max_connections = 10240
max_connect_errors = 10000  
open_files_limit = 10240 
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
thread_cache_size = 64  ##线程缓存  
query_cache_size = 32M  ##查询缓存大小
query_cache_limit = 2M
key_buffer_size = 4M
ft_min_word_len = 4
transaction_isolation = REPEATABLE-READ
log_bin = mysql-bin  
binlog_format = mixed 
expire_logs_days = 30 
performance_schema = 0
explicit_defaults_for_timestamp
skip-external-locking
default-storage-engine = InnoDB
innodb_file_per_table = 1 
innodb_open_files = 1024
innodb_buffer_pool_size = 512M  
innodb_write_io_threads = 4  
innodb_read_io_threads = 4 
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 = 32M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1

relay_log_info_repository = TABLE

[mysqldump]
quick
max_allowed_packet = 16M 

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

[mysql]
#character_set_server=utf8

六、授权用户

chown -R mysql:mysql /mysql/app
chown -R mysql:mysql /mysql/app/data
chmod 1777 /mysql/app/tmp

七、设置环境变量

echo 'export PATH=/mysql/app/bin:$PATH' >>/etc/profile
source !$

八、创建服务启动脚本,添加开机启动

cp /mysql/app/support-files/mysql.server/etc/init.d/mysqld 
chmod +x /etc/init.d/mysqld //给脚本添加执行权限 
vi /etc/init.d/mysqld

//服务启动脚本要修改以下两个参数 
basedir=/mysql/app //MySQL安装目录 
datadir= /mysql/app/data //数据存放目录
conf=/mysql/etc/my.cnf  //配置文件路径
chkconfig mysqld on //添加开机启动

九、初始化数据库

 /mysql/app/bin/mysqld --initialize --user=mysql --basedir=/mysql/app --datadir=/mysql/app/data/
  • /mysql/app/logs/mysql-error.log
  • 记录临时密码

十、重启mysql

service mysqld restart

十一、登陆

  • mysql -u root -p
  • 输入临时密码
  • 修改root密码
  • SET PASSWORD = PASSWORD('新密码');
  • ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
  • flush privileges;