CentOS 6下安装MySQL5.6

1. 下载MySQL软件

https://dev.mysql.com/downloads/mysql/5.6.html#downloads

 

2. 解压缩

cd /usr/local

tar xzvf /tmp/mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz /usr/local

mv mysql-5.6.31-linux-glibc2.5-x86_64 mysql

chown -R mysql:mysql /usr/local/mysql/

 

3. 创建MySQL用户

useradd  mysql 

id mysql
uid=500(mysql) gid=500(mysql) groups=500(mysql)

 

4. 创建数据目录

数据存放目录:/data/mysql/mysql_3306/{data, logs,tmp}

mkdir –p /data/mysql/mysql_3306/{data,logs,tmp}

chown -R mysql:mysql /data/mysql/

 

5. 添加环境变量

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

source /etc/profile

 

6. 编辑 my.cnf

[client]
user                            = root
password                        = 'Abc_1234'
port                            = 3306
socket                          = /data/mysql/mysql_3306/tmp/mysql.sock

[mysql]

# CLIENT #
port                            = 3306
socket                          = /data/mysql/mysql_3306/tmp/mysql.sock
default_character_set           = utf8
prompt="\u@\h:\p  \R:\m:\s [\d]>"
                                
[mysqld]                        
                                
# GENERAL #                     
user                            = mysql
default_storage_engine          = InnoDB
character_set_server            = utf8mb4
socket                          = /data/mysql/mysql_3306/tmp/mysql.sock
pid_file                        = /data/mysql/mysql_3306/logs/mysqld.pid
server_id                       = 1001
explicit_defaults_for_timestamp = 1
back_log                        = 500
                                
# MyISAM #                      
key_buffer_size                 = 32M
myisam_recover_options          = FORCE,BACKUP
                                
# SAFETY #                      
max_allowed_packet              = 32M
max_connect_errors              = 100000
sql_mode                        = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
innodb                          = FORCE

# TIMEOUT #
interactive_timeout             = 600
wait_timeout                    = 600

# DATA STORAGE #
datadir                         = /data/mysql/mysql_3306/data

# BINARY LOG #
log_bin                         = /data/mysql/mysql_3306/logs/mysql_bin
expire_logs_days                = 15
sync_binlog                     = 1
binlog_format                   = ROW
log_bin_trust_function_creators = 1
binlog_rows_query_log_events    = 1
binlog_cache_size               = 4M
max_binlog_size                 = 1G

# RELAY LOG #
relay_log                      = /data/mysql/mysql_3306/logs/relay_bin
max_relay_log_size             = 1G
relay_log_info_repository      = TABLE
master_info_repository         = TABLE
relay_log_recovery             = 1


# REPLICATI #
gtid_mode                      = 1
enforce_gtid_consistency       = 1
binlog_gtid_simple_recovery    = 1
log_slave_updates              = 1
sync_relay_log                 = 10000
sync_relay_log_info            = 10000
sync_master_info               = 1
#slave_skip_errors             = ddl_exist_errors

# SLAVE #
#read_only                     = 1
slave_net_timeout              = 60
#skip_slave_start              = 1
slave_pending_jobs_size_max    = 64M

# CACHES AND LIMITS #
tmp_table_size                 = 64M
max_heap_table_size            = 32M
sort_buffer_size               = 2M
join_buffer_size               = 2M
read_buffer_size               = 4M
read_rnd_buffer_size           = 4M
bulk_insert_buffer_size        = 32M
query_cache_type               = 0
query_cache_size               = 0
max_connections                = 500
thread_cache_size              = 50
open_files_limit               = 65535
table_definition_cache         = 4096
table_open_cache               = 4096

# INNODB #
innodb_flush_method            = O_DIRECT
innodb_log_files_in_group      = 4
innodb_log_file_size           = 512M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table          = 1
innodb_log_buffer_size         = 16M
innodb_buffer_pool_size        = 8G
innodb_data_file_path          = ibdata1:1G:autoextend
innodb_io_capacity             = 4000
innodb_io_capacity_max         = 8000
innodb_lru_scan_depth          = 2000
innodb_lock_wait_timeout       = 50
innodb_undo_logs               = 128
innodb_undo_tablespaces        = 3
innodb_purge_threads           = 4
innodb_print_all_deadlocks     = 1
innodb_strict_mode             = 1
innodb_sort_buffer_size        = 64M 
innodb_flush_neighbors         = 1 # 固态硬盘可以设置为0
innodb_buffer_pool_load_at_startup  = 1
innodb_buffer_pool_dump_at_shutdown = 1
         

# LOGGING #
log_error                      = /data/mysql/mysql_3306/logs/mysql_error.log
log_error_verbosity            = 1
slow_query_log                 = 1
slow_query_log_file            = /data/mysql/mysql_3306/logs/mysql_slow.log
max_slowlog_size               = 1G
log_queries_not_using_indexes  = 0
log_warnings                   = 2
long_query_time                = 1
log_slow_admin_statements      = 1
log_slow_slave_statements      = 1
min_examined_row_limit         = 100000

 mysql加载my.cnf的顺序

使用mysql --verbose --help | grep my.cnf命令可以查看mysql加载配置文件都顺序

/etc/my.cnf --> /etc/mysql/my.cnf --> /usr/local/mysql/etc/my.cnf --> /usr/local/mysql/etc/my.cnf --> ~/.my.cnf

最后面的变量覆盖前面都变量

mysql --print-defaults
查看mysql当前的配置参数

mysqld --defaults-file=/etc/my.cnf
指定读取哪个配置文件

 

7. 初始化数据库

cd /usr/local/mysql/

./scripts/mysql_install_db --defaults-file=/etc/my.cnf
Installing MySQL system tables...2016-06-04 15:19:37 0 [Note] ./bin/mysqld (mysqld 5.6.31-log) starting as process 3305 ...
OK

Filling help tables...2016-06-04 15:19:43 0 [Note] ./bin/mysqld (mysqld 5.6.31-log) starting as process 3328 ...
OK
…………

看到两个OK,证明mysql安装成功。再查看/data/mysql/msyql_3306/data/error.log,确认没有报错。

MySQL5.7 使用 mysqld 初始化数据库,mysqld --defaults-file=/etc/my.cnf

 

8. 启动MySQL

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

service mysqld start
Starting MySQL.. [ OK ]

看到OK,证明mysql启动成功。

查看MySQL进程是否存在及监听端口

ps axu|grep mysql

root 3382 0.0 0.1 11468 1380 pts/1 S 15:24 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/mysql_3306/data --pid-file=/data/mysql/mysql_3306/data/mysql.pid
mysql 4320 0.4 55.9 1160676 570620 pts/1 Sl 15:24 0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/mysql_3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql_3306/data/error.log --open-files-limit=65535 --pid-file=/data/mysql/mysql_3306/data/mysql.pid --socket=/data/mysql/mysql_3306/tmp/mysql.sock --port=3306
root 4371 0.0 0.0 103252 840 pts/1 S+ 15:28 0:00 grep mysql

netstat -nalp|grep "3306"
tcp 0 0 :::3306 :::* LISTEN 4320/mysqld 
unix 2 [ ACC ] STREAM LISTENING 33638 4320/mysqld /data/mysql/mysql_3306/tmp/mysql.sock

lsof -n -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 4320 mysql 14u IPv6 33637 0t0 TCP *:mysql (LISTEN)

service mysqld start --> mysqld_safe --> mysqld

service mysqld start 调用 mysqld_safe, mysqld_safe再调用mysqld

 

9. 删除无用账号

delete from mysql.user where user!='root' or host!='localhost';
select user,host,password from mysql.user;
+------+-----------+----------+
| user | host | password |
+------+-----------+----------+
| root | localhost | |
truncate table mysql.db;

 

posted @ 2018-10-15 17:58  Ziroro  阅读(238)  评论(0编辑  收藏  举报