centos6环境下安装mysql

166 阅读3分钟

#安装wget包

yum -y install libaio libaio-devel

#时区和时间同步

cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

/usr/sbin/ntpdate 0.cn.pool.ntp.org

添加mysql用户

添加群组: groupadd mysql
添加用户: useradd -g mysql -d /usr/local/mysql -s /sbin/nologin -M -n mysql

关闭swap

vim  /etc/sysctl.conf
在最后一行加上vm.swappiness=0
sysctl -p

#一.安装mysql ##目录赋权限

创建mysql目录:mkdir -p /opt/environment/ && cd /opt/environment/
下载mysql(已经下载好请跳过这步): wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz 
解压安装包: tar -zvxf /opt/packages/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz 
创建软连:ln -s /opt/environment/mysql-5.7.23-linux-glibc2.12-x86_64 /usr/local/mysql
 mkdir -p /data/db/mysql/{3316/{data,logs,tmp},backup,scripts/log}
 chown -R mysql:mysql /data/db/mysql
 chown -R mysql:mysql /usr/local/mysql
 chown -R mysql:mysql /opt/environment/mysql-5.7.23-linux-glibc2.12-x86_64/

##配置mysql

创建配置文件: touch /etc/my.cnf
给配置文件赋权限: chmod 777 /etc/my.cnf
编辑配置文件: vim /etc/my.cnf
配置文件内容:

[client]
port  = 3316
socket =  /tmp/mysql3316.sock

[mysql]
prompt="\u@HOSTNAME \R:\m:\s [\d]> "
no-auto-rehash

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /data/db/mysql/3316/data
port = 3316
socket = /tmp/mysql3316.sock
character-set-server = utf8mb4
skip_name_resolve = 1
open_files_limit = 65535
back_log = 600
max_connections =1000
max_connect_errors = 1000000
table_open_cache = 1000
secure_file_priv=/tmp/
max_allowed_packet = 256M
lower_case_table_names =1
event_scheduler = 0
default_password_lifetime=0
tmpdir = /data/db/mysql/3316/tmp
log_timestamps=system
explicit_defaults_for_timestamp=1
interactive_timeout = 1800
wait_timeout = 1800
lock_wait_timeout = 3600
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
thread_cache_size = 32
tmp_table_size = 32M
key_buffer_size = 8M
read_buffer_size = 4M
read_rnd_buffer_size = 8M
bulk_insert_buffer_size = 8M
sort_buffer_size = 256K
join_buffer_size = 256K
 
slow_query_log = 1
slow_query_log_file = slow.log
log-error = error.log
log_warnings = 2
pid-file = mysql.pid
long_query_time = 2
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1

 
server-id = 3316
log-bin = /data/db/mysql/3316/logs/mysql-bin
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_size = 1G
max_binlog_cache_size = 2G
expire_logs_days = 7

plugin_dir=/usr/local/mysql/lib/plugin
plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 3000

gtid-mode = on
enforce-gtid-consistency=1
log_slave_updates

binlog_format = row
skip_slave_start = 1
max_relay_log_size = 128M
relay_log_purge = 1
relay-log=relay-bin
relay-log-index=relay-bin.index

myisam_repair_threads = 1
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 1G


innodb_file_per_table = 1
innodb_print_all_deadlocks = 1

innodb_stats_on_metadata = 0

transaction_isolation = READ-COMMITTED
innodb_flush_method = O_DIRECT

innodb_buffer_pool_size = 8G
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_file_size = 1G
innodb_log_files_in_group = 3

innodb_log_group_home_dir = /data/db/mysql/3316/data
innodb_undo_directory = /data/db/mysql/3316/data
innodb_undo_logs = 128
innodb_undo_tablespaces = 3
innodb_undo_log_truncate = 1
innodb_flush_neighbors = 0

slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=8
master_info_repository = TABLE
relay_log_info_repository = TABLE
relay_log_recovery = 1

binlog_group_commit_sync_delay=1000
binlog_group_commit_sync_no_delay_count=50
slave_preserve_commit_order = ON

show_compatibility_56=on

[mysqldump]
quick
max_allowed_packet = 32M

撤销配置文件编辑权限: chmod 644 /etc/my.cnf

配置mysql环境变量

echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
source /etc/profile

##初始化mysql

/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --datadir=/data/db/mysql/3316/data  --user=mysql --initialize

##启动mysql

/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &

##首次登陆mysql

查看root初始密码: cat /data/db/mysql/3316/data/error.log |grep "root@localhost"|awk -F " " '{print $11}' |head -n 1
登陆mysql: mysql -uroot -p -P3316 -S /tmp/mysql3316.sock
修改root密码: alter user user() identified by 'yzw@com';
重新登陆mysql: mysql -uroot -p'yzw@com' -P3316 -S /tmp/mysql3316.sock

##创建应用数据库

root登陆mysql执行以下sql语句
CREATE DATABASE mydb;

##创建应用数据库用户

root登陆mysql执行以下sql语句
创建用户: create user 'app_owner'@'%' identified by 'app_owner@123456';
给用户赋予权限: grant all privileges on mydb.* to app_owner@'%';
刷新权限: flush privileges;

##打开mysql端口

查看活跃区: firewall-cmd --get-active-zones
开放端口: firewall-cmd --zone=public --add-port=3316/tcp --permanent
重启防火墙: firewall-cmd --reload

##设置开机启动

复制服务到启动目录: cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
设置启动方式: echo "/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &" >>/etc/rc.local
设置执行权限 : chmod +x /etc/rc.d/rc.local

关闭mysqld及禁止自动启动

service mysqld status
service mysqld stop

# 关闭自动启动
chkconfig mysqld off
chkconfig --list | grep mysqld

关闭mysqld

附: 简单说明下 centos 6 与 centos 7 服务开机启动、关闭设置的方法:

 centos 6 :使用chkconfig命令即可。

我们以apache服务为例:

#chkconfig --add apache 添加nginx服务

#chkconfig apache on 开机自启nginx服务

#chkconfig apache off 关闭开机自启

#chkconfig --list | grep apache 查看

centos 7 :使用systemctl中的enable、disable 即可。
示例:

#systemctl enable apache.service 开机自启apache服务

#systemctl disable apache.service 关闭开机自启

#systemctl list-unit-files|grep iptables 查看iptables是否开机启动的状态

参考资料