Centos7 安装 MySQL5.7
Posted UnixFBI 运维特工
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7 安装 MySQL5.7相关的知识,希望对你有一定的参考价值。
Centos7 安装 mysql5.7
一、环境介绍
1.安装包版本介绍
MySQL 有三种安装方式:RPM安装、二进制包安装、源码包安装。我们这篇文章以二进制方式安装MySQL
| 软件名称 | 版本 |
| :-------- | :--------:|
| 系统版本 | CentOS Linux release 7.2.1511 (Core) |
| MySQL | mysql-5.7.18-linux-glibc2.5-x86_64|
2.二进制包相关目录介绍
MySQL Installation Layout for Generic Unix/Linux Binary Package
| Directory | Contents of Directory |
| :-------- | :--------:|
| bin |mysqld server, client and utility programs |
|docs |MySQL manual in Info format |
| man | Unix manual pages |
|include | Include (header) files |
| lib | Libraries |
| share | Error messages, dictionary, and SQL for database installation |
| support-files | Miscellaneous support files|
3.安装目录规划
配置项 | 说明 |
---|---|
config | /etc/my.cnf |
datadir | /data/mysql/mysql3306/data |
binlogdir | /data/mysql/mysql3306/logs |
tmpdir | /data/mysql/mysql3306/tmp |
/data 在生产环境中这个目录最好是一个单独挂载的一个分区
server-id 为了防止server-id冲突,我们规定是主机IP地址的最后一位+MySQL监听的端口号。例如我的IP是192.168.1.100 MySQL监听的端口为3306 ,所以我的server-id = 1003306
二、安装MySQL
1.安装相关依赖
# yum install libaio -y
2.下载MySQL二进制安装包
国内源:
# wget -P /opt/ http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
国外源:
# wget -P /opt/ https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
3.创建用户
# useradd -s /sbin/nologin -M mysql
4.解压软连接MySQL二进制包
# mkdir /opt/mysql/
# tar zxf /opt/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /opt/mysql/
# cd /usr/local/
# ln -s /opt/mysql/mysql-5.7.18-linux-glibc2.5-x86_64 mysql
5.创建数据库相关目录
# mkdir -pv /data/mysql/mysql3306/{data,logs,tmp}
6.修改相关目录权限
# chown -R mysql.mysql /usr/local/mysql
# chown -R mysql.mysql /data
7.创建配置文件
我们使用的配置文件为:/etc/my.cnf
# cat > /etc/my.cnf << EOF
###### base ######
#my.cnf
[client]
port = 3306
socket = /tmp/mysql3306.sock
[mysql]
prompt="\[email protected]\h [\d]>"
no-auto-rehash
[mysqld]
#misc
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/mysql3306/data
tmpdir = /data/mysql/mysql3306/tmp
port = 3306
socket = /tmp/mysql3306.sock
event_scheduler = 0
#timeout
interactive_timeout = 300
wait_timeout = 300
#character set
character-set-server = utf8
open_files_limit = 65535
max_connections = 100
max_connect_errors = 100000
lower_case_table_names =1
###### GTID ######
gtid-mode = on
enforce-gtid-consistency=1
###### symi replication ######
#rpl_semi_sync_master_enabled=1
#rpl_semi_sync_master_timeout=1000 # 1 second
#rpl_semi_sync_slave_enabled=1
####### slow log ######
log-output=file
slow_query_log = 1
slow_query_log_file = slow.log
log-error = error.log
log_warnings = 2
pid-file = mysql.pid
long_query_time = 1
#log-slow-admin-statements = 1
#log-queries-not-using-indexes = 1
log-slow-slave-statements = 1
####### binlog ######
binlog_format = row
server-id = 1003306
log-bin = /data/mysql/mysql3306/logs/mysql-bin
max_binlog_size = 256M
sync_binlog = 0
expire_logs_days = 10
#procedure
log_bin_trust_function_creators=1
####### relay log ######
skip_slave_start = 1
max_relay_log_size = 128M
relay_log_purge = 1
relay_log_recovery = 1
relay-log=relay-bin
relay-log-index = relay-bin.index
log_slave_updates = ON
#slave-skip-errors=1032,1053,1062
#skip-grant-tables
####### buffers & cache ######
table_open_cache = 2048
table_definition_cache = 2048
table_open_cache = 2048
max_heap_table_size = 96M
sort_buffer_size = 128K
join_buffer_size = 128K
thread_cache_size = 200
query_cache_size = 0
query_cache_type = 0
query_cache_limit = 256K
query_cache_min_res_unit = 512
thread_stack = 192K
tmp_table_size = 96M
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 32M
#myisam
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
#innodb
innodb_buffer_pool_size = 100M
innodb_buffer_pool_instances = 1
innodb_data_file_path = ibdata1:100M:autoextend
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 8M
innodb_log_file_size = 100M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 50
innodb_file_per_table = 1
innodb_rollback_on_timeout
innodb_io_capacity = 2000
transaction_isolation = READ-COMMITTED
innodb_flush_method = O_DIRECT
EOF
8.初始化数据库
# cd /usr/local/mysql
# ./bin/mysqld --defaults-file=/etc/my.cnf --initialize
9.创建MySQL启动脚本
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# /etc/init.d/mysqld start
10.创建环境变量
# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
# source /etc/profile
11.修改初始密码
查看初始密码:
# grep "password" /data/mysql/mysql3306/data/error.log
2017-05-08T07:49:40.620503Z 1 [Note] A temporary password is generated for root@localhost: q=8jh*JpNar)
初始密码为: q=8jh*JpNar) 每次初始化密码都不会相同;
登录数据库修改密码为:unixfbi.com
# mysql -uroot -p初始密码
mysql> alter user user() identified by ‘unixfbi.com‘;
或者:
mysql> SET PASSWORD=PASSWORD(‘unixfbi.com‘);
mysql> flush privileges;
或者:
# PASSWD=$(grep ‘password is‘ /data/mysql/mysql3306/data/error.log | awk ‘{print $NF}‘)
# mysql -uroot -p"$PASSWD" --connect-expired-password -e "alter user user() identified by ‘unixfbi.com‘;"
参考文档
https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
http://database.51cto.com/art/201108/285365.htm
本文出自 “运维特工” 博客,转载请务必保留原文链接 和 http://www.unixfbi.com
以上是关于Centos7 安装 MySQL5.7的主要内容,如果未能解决你的问题,请参考以下文章