安装MySQL5.7
拓展包安装
yum install -y libaio-devel.x86_64 numactl
编译安装
# 下载安装包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
# 解压
tar -zxvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
# 将解压文件移动道/usr/local
mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql
# 创建mysql的data目录
mkdir -vp /data/mysql
# 创建mysql组
groupadd mysql
# 创建mysql用户
useradd -r -g mysql mysql
# 给mysql目录赋予权限,用户以及用户组
chown -R mysql:mysql /usr/local/mysql/ /data/mysql/
chmod -R 755 /usr/local/mysql/ /data/mysql/
# 编译安装
cd /usr/local/mysql/bin
./mysqld --initialize --explicit_defaults_for_timestamp --user=mysql --datadir=/data/mysql/ --basedir=/usr/local/mysql/
# 返回初始密码
[Note] A temporary password is generated for root@localhost: lGvk#,plA1##
修改配置
vi /etc/my.cnf
# 将datadir改成自定义的data目录 socket默认指的路径无权限.不改会报错
datadir=/data/mysql/
socket = /tmp/mysql.sock
启动
重制密码
/usr/local/mysql/support-files/mysql.server start
# 进入mysql,密码为编译安装返回的密码
/usr/local/mysql/bin/mysql -uroot -p
# 设置新密码
set password for root@localhost = password('qvbilam');
添加软连接
# 设置软连接
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
# 重启测试
service mysql restart
开机自动启动
# 复制mysql.server到/etc/rc.d/init.d/mysqld
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
# 添加执行权限
chmod +x /etc/init.d/mysqld
# 添加服务
chkconfig --add mysqld
# 查看345是否为开
chkconfig --list
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
如果345为关在执行下面代码
# 注意提示看是否需要执行
chkconfig --level 345 mysqld on
允许远程连接
设置MySQL
-- 允许远程连接
update user set user.Host='%' where user.User='root';
-- 权限生效
flush privileges;
设置iptables规则
# 安装防火墙
yum install -y iptables iptables-services
# 停止并禁用自带的防火墙
systemctl stop firewalld
systemctl mask firewalld
# 启动iptables
service iptables start
# 修改规则
vi /etc/sysconfig/iptables
# 添加
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
# 重启生效
service iptables restart
# 其他服务器连接测试
mysql -h192.168.128.135 -uroot -p
My.cnf配置
以下是个人使用的my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysql]
prompt="MySQL [\d]> "
no-auto-rehash
[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 = 4
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 324
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
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 = 7
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 = 500M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M