1. 程式人生 > >MySQL 5.7.22 二進位制包部署單例項

MySQL 5.7.22 二進位制包部署單例項

MySQL 5.7.22二進位制包部署單例項

1.1 前期規劃... 1

1.2 軟體下載... 1

1.3 系統環境... 1

1.4 安裝程式... 1

1.5 準備配置檔案... 3

1.6 準備啟動指令碼... 5

1.7 初始化mysql並完成安裝... 8

1.8 加入開機自啟動... 9

1.1 前期規劃

01:虛擬機器一臺,2vCPU,4G記憶體,部署mysql服務,埠3306。

02:MySQL程式的安裝目錄:/data/apps/mysql-5.7.22 要做軟體連結:/data/apps/mysql

03:配置檔案路徑為:/data/mysql/3306/my.cnf

04:其它相關目錄:/data/mysql/3306/{data,binlog,slowlog,errlog,redolog }

05:採用MySQL通用二進位制包(mysql-5.7.22-linux-glibc2.12-x86_64.tar)

1.2 軟體下載

連結:https://pan.baidu.com/s/1Q6jl8UcoLCIbMShZREBSKw

提取碼:litr

1.3 系統環境

[[email protected] ~]# cat /etc/redhat-release;uname -r;uname -m

CentOS release 6.8 (Final)

2.6.32-642.el6.x86_64

x86_64

[[email protected] ~]# /etc/init.d/iptables status

iptables: Firewall is not running.

[[email protected] ~]# getenforce

Disabled

[[email protected] ~]# ifconfig eth0|awk -F "[ :]+" 'NR==2{print $4}'

10.0.0.10

[[email protected] ~]# ifconfig eth1|awk -F "[ :]+" 'NR==2{print $4}'

172.16.1.10

[[email protected] ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda3 26G 1.5G 23G 6% /

tmpfs 1.9G 0 1.9G 0% /dev/shm

/dev/sda1 976M 38M 888M 5% /boot

/dev/sdb1 50G 52M 47G 1% /data

1.4 安裝程式

##解除安裝系統自帶的mysql相關程式

[[email protected] ~]# rpm -qa|grep mysql

mysql-libs-5.1.73-7.el6.x86_64

[[email protected] ~]# rpm -e --nodeps mysql-libs-5.1.73-7.el6.x86_64

[[email protected] ~]# rpm -qa|grep mysql

[[email protected] ~]#

##檢查是否有libaio軟體包(如果沒有則用yum install libaio -y命令來安裝,前提是要能上網)

[[email protected] ~]# rpm -qa|grep libaio

libaio-0.3.107-10.el6.x86_64

##建立mysql使用者,不讓其登入,不建立家目錄

[[email protected] ~]# useradd -s /sbin/nologin -M mysql

[[email protected] ~]# id mysql

uid=501(mysql) gid=501(mysql) groups=501(mysql)

[[email protected] ~]# tail -1 /etc/passwd

mysql:x:501:501::/home/mysql:/sbin/nologin

##建立相關的目錄

[[email protected] ~]# mkdir /data/apps /data/mysql/3306/{data,binlog,errlog,slowlog,redolog} -p

[[email protected] ~]# tree /data/

/data/

|-- apps

|-- lost+found

`-- mysql

`-- 3306

|-- binlog

|-- data

|-- errlog

|-- redolog

`-- slowlog

9 directories, 0 files

#建立/tools目錄,我們把軟體上傳到這個目錄下,然後進行操作

[[email protected] ~]# mkdir tools

[[email protected] ~]# cd tools/

[[email protected] tools]# rz

[[email protected] tools]# ll mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

-rw-r--r-- 1 root root 643790848 Nov 21 09:47 mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

[[email protected] tools]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /data/apps/

[[email protected] tools]# mv /data/apps/mysql-5.7.22-linux-glibc2.12-x86_64/ /data/apps/mysql-5.7.22

[[email protected] tools]# ln -s /data/apps/mysql-5.7.22/ /data/apps/mysql

[[email protected] tools]# ll /data/apps/mysql

lrwxrwxrwx 1 root root 24 Dec 10 10:44 /data/apps/mysql -> /data/apps/mysql-5.7.22/

[[email protected] tools]# chown -R mysql:mysql /data/apps/mysql/

[[email protected] ~]# echo "PATH=/data/apps/mysql/bin:$PATH" >>/etc/bashrc

[[email protected] ~]# source /etc/bashrc

[[email protected] ~]# echo $PATH

/data/apps/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

1.5 準備配置檔案

[[email protected] tools]# vim /data/mysql/3306/my.cnf

[client]

port=3306

socket=/data/mysql/3306/mysql.sock

[mysql]

no_auto_rehash

[mysqld]

###Basic Parameters

user=mysql

port=3306

basedir=/data/apps/mysql

datadir=/data/mysql/3306/data

socket=/data/mysql/3306/mysql.sock

pid_file=/data/mysql/3306/mysql.pid

character_set_server=utf8

skip_character_set_client_handshake=1

###Server id

server_id=3306

###Binlog Parameters

log_output=FILE

log_timestamps=system

binlog_format=row

expire_logs_days=15

max_binlog_size=2M

binlog_cache_size=2M

max_binlog_cache_size=4M

sync_binlog=1

master_info_repository=table

binlog_rows_query_log_events=on

log_bin_trust_function_creators=1

log_bin=/data/mysql/3306/binlog/mysql_bin

###Slowlog Parameters

log_slow_admin_statements=1

slow_query_log=on

long_query_time=2

log_queries_not_using_indexes=on

slow_query_log_file=/data/mysql/3306/slowlog/mysql_slow.log

###Errlog Parameters

log-error=/data/mysql/3306/errlog/mysql_error.log

###Other Parameters

autocommit=on

skip_external_locking=on

skip_name_resolve=on

max_connections=800

max_connect_errors=1000

max_allowed_packet=200M

wait_timeout=1800

interactive_timeout=1800

open_files_limit=65535

group_concat_max_len=4294967295

symbolic_links=0

transaction_write_set_extraction=off

transaction_isolation=READ-COMMITTED

explicit_defaults_for_timestamp=1

sql_mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"

###Innodb Parameters

default_storage_engine=InnoDB

innodb_flush_log_at_trx_commit=1

innodb_buffer_pool_size=500M

innodb_buffer_pool_instances=8

innodb_buffer_pool_dump_at_shutdown=1

innodb_buffer_pool_dump_pct=40

innodb_buffer_pool_load_at_startup=1

innodb_file_per_table=1

innodb_change_buffering=all

innodb_doublewrite=on

innodb_autoextend_increment=64

innodb_temp_data_file_path=ibtmp1:200M:autoextend:max:20G

innodb_data_file_path=ibdata1:200M;ibdata2:200M;ibdata3:200M:autoextend:max:20G

innodb_flush_method=O_DIRECT

innodb_log_buffer_size=16M

innodb_log_file_size=4G

innodb_log_files_in_group=2

innodb_log_group_home_dir=/data/mysql/3306/redolog

innodb_undo_logs=128

innodb_undo_tablespaces=3

innodb_undo_log_truncate=1

innodb_max_undo_log_size=20G

innodb_purge_rseg_truncate_frequency=128

innodb_print_all_deadlocks=on

innodb_lock_wait_timeout=5

innodb_deadlock_detect=on

innodb_status_output_locks=on

innodb_strict_mode=1

innodb_sort_buffer_size=64M

innodb_open_files=65535

innodb_concurrency_tickets=5000

innodb_page_cleaners=4

innodb_old_blocks_time=1000

innodb_stats_on_metadata=0

innodb_checksum_algorithm=0

show_compatibility_56=on

innodb_lru_scan_depth=2000

innodb_flush_neighbors=1

innodb_purge_threads=4

innodb_large_prefix=1

1.6 準備啟動指令碼

注意:

01:該指令碼中停止mysql的方法為:

mysqladmin -uUser -pPassword -S /data/mysql/3306/mysql.sock shutdown

02:指令碼中有一個Pass變數,就是MySQL root使用者的密碼

03:當然你也可以設定成其它密碼,但必須得改指令碼中的Pass變數的值(前提是用這個指令碼)

[[email protected] ~]# vim /data/mysql/3306/mysqld

#!/bin/bash

#

# MySQL boot script

# QQ:158316096

# Blog:http://blog.51cto.com/11576296

#

# Define variables

RETVAL=0

Port=3306

User=root

Pass=chenliang

Pid=/data/mysql/3306/mysql.pid

Sock=/data/mysql/3306/mysql.sock

My=/data/mysql/3306/my.cnf

Path=/data/apps/mysql/bin

# Determine the user to execute

if [ $UID -ne $RETVAL ];then

echo "Must be root to run scripts"

exit 1

fi

# Load the local functions

[ -f /etc/init.d/functions ] && source /etc/init.d/functions

# Define functions

start(){

if [ ! -f "$Pid" ];then

$Path/mysqld_safe --defaults-file=$My >/dev/null 2>&1 &

RETVAL=$?

if [ $RETVAL -eq 0 ];then

action "Start MySQL [3306]" /bin/true

else

action "Start MySQL [3306]" /bin/false

fi

else

echo "MySQL 3306 is running"

exit 1

fi

return $RETVAL

}

stop(){

if [ -f "$Pid" ];then

$Path/mysqladmin -u$User -p$Pass -S $Sock shutdown >/dev/null 2>&1

RETVAL=$?

if [ $RETVAL -eq 0 ];then

action "Stop MySQL[3306]" /bin/true

else

action "Stop MySQL[3306]" /bin/false

fi

else

echo "MySQL [3306] is not running"

exit 1

fi

return $RETVAL

}

status(){

if [ -f "$Pid" ];then

echo "MySQL [3306] is running"

else

echo "MySQL [3306] is not running"

fi

return $RETVAL

}

# Case call functions

case "$1" in

start)

start

RETVAL=$?

;;

stop)

stop

RETVAL=$?

;;

restart)

stop

sleep 5

start

RETVAL=$?

;;

status)

status

RETVAL=$?

;;

*)

echo "USAGE:$0{start|stop|restart|status}"

exit 1

esac

# Scripts return values

exit $RETVAL

1.7 初始化mysql並完成安裝

#更改/data/mysql/3306/mysqld指令碼的許可權為700(因為指令碼中有密碼)

[[email protected] ~]# chmod 700 /data/mysql/3306/mysqld

[[email protected] ~]# ll /data/mysql/3306/mysqld

-rwx------ 1 root root 1897 Dec 9 00:44 /data/mysql/3306/mysqld

#建立錯誤日誌

[[email protected] ~]# grep "errlog" /data/mysql/3306/my.cnf

log-error=/data/mysql/3306/errlog/mysql_error.log

[[email protected] ~]# touch /data/mysql/3306/errlog/mysql_error.log

[[email protected] ~]# ll /data/mysql/3306/errlog/mysql_error.log

-rw-r--r-- 1 root root 0 Dec 10 10:54 /data/mysql/3306/errlog/mysql_error.log

#更改/data/mysql/3306/目錄及子目錄下檔案的屬主/組為mysql

[[email protected] ~]# chown -R mysql:mysql /data/mysql/3306/

[[email protected] ~]# ls -ld /data/mysql/3306/

drwxr-xr-x 7 mysql mysql 4096 Dec 10 10:50 /data/mysql/3306/

#初始化mysql

[[email protected] ~]# mysqld --defaults-file=/data/mysql/3306/my.cnf --user=mysql --basedir=/data/apps/mysql --datadir=/data/mysql/3306/data/ --initialize

[[email protected] ~]# echo $?

0

#啟動mysql服務並修改密碼為chenliang

[[email protected] ~]# /data/mysql/3306/mysqld start

Start MySQL [3306] [ OK ]

[[email protected] ~]# netstat -lntup|grep 3306

tcp 0 0 :::3306 :::* LISTEN 3384/mysqld

[[email protected] ~]# grep "password" /data/mysql/3306/errlog/mysql_error.log

2018-12-10T11:01:25.394681+08:00 1 [Note] A temporary password is generated for [email protected]: z5D/BEBrcil?

[[email protected] ~]# mysql -uroot -p -S /data/mysql/3306/mysql.sock

Enter password: <=輸入密碼z5D/BEBrcil?

mysql>

mysql> alter user 'root'@'localhost' identified by 'chenliang';

Query OK, 0 rows affected (0.03 sec)

mysql> select user,host,authentication_string,password_expired from mysql.user where user='root' and host='localhost';

+------+-----------+-------------------------------------------+------------------+

| user | host | authentication_string | password_expired |

+------+-----------+-------------------------------------------+------------------+

| root | localhost | *D072DB593E2E7B068E887A452EB1DB3981A5D844 | N |

+------+-----------+-------------------------------------------+------------------+

1 row in set (0.02 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

mysql> exit

Bye

[[email protected] ~]# mysql -uroot -p -S /data/mysql/3306/mysql.sock -e "show databases;"

Enter password: <==輸入密碼chenliang

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

+--------------------+

1.8 加入開機自啟動

[[email protected] ~]# echo -ne "\n# Boot start mysql service[3306].USER:chenliang TIME:2018-11-23\n/data/mysql/3306/mysqld start\n" >>/etc/rc.local

[[email protected] ~]# tail -2 /etc/rc.local

# Boot start mysql service[3306].USER:chenliang TIME:2018-11-23

/data/mysql/3306/mysqld start