1. 程式人生 > >yum && 編譯 安裝mysql 5.7 多實例

yum && 編譯 安裝mysql 5.7 多實例

name mon symbol server-id service lin extra safe min

yum安裝

[[email protected] ~]# wget http://repo.mysql.com/mysql57-community-release-el7.rpm
[[email protected] ~]# rpm -ivh mysql57-community-release-el7.rpm
[root@localhost ~]# yum install mysql mysql-server

初始化

[[email protected] ~]# grep "password" /var/log/mysqld.log 
2017-04-12T00:45:32.972626Z 1 [Note] A temporary password is generated for [email protected]
/* */: kvyqpEMl<9b0 #隨機密碼
[[email protected] ~]# mysqladmin -u root -p password   #重置密碼
Enter password: 
New password: 
Confirm new password: 
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld

編譯安裝

環境準備

[[email protected] ~]yum install -y gcc gcc-c++ openssl openssl-devel ncurses ncurses-devel cmake bison*
[root@localhost ~]# mkdir /data/
[root@localhost ~]# mkdir /data/mysql/
[root@localhost ~]# mkdir /data/mysql/data/
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -g mysql -s /sbin/nologin mysql
[root@localhost ~]# cd /usr/local/src/
[root@localhost ~]# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz
[root@localhost src]# tar zvxf mysql-boost-5.7.18.tar.gz 
[root@localhost src]# cd mysql-5.7.18

編譯安裝

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/data -DSYSCONFDIR=/etc -DWITH_BOOST=boost -DEFAULT_CHARSET=utf8-DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DMYSQL_UNIX_ADDR=/data/mysql/data/mysqld.sock -DENABLED_LOCAL_INFILE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_TCP_PORT=3306

# -DCMAKE_INSTALL_PREFIX=/usr/local/mysql              #安裝路徑
# -DMYSQL_DATADIR=/data/mysql/data                     #數據文件存放位置
# -DSYSCONFDIR=/etc                                    #my.cnf路徑
# -DWITH_BOOST=boost                                   #指定boost庫 
# -DDEFAULT_CHARSET=utf8                               #默認字符
# -DDEFAULT_COLLATION=utf8_general_ci                  #默認排序規則
# -DEXTRA_CHARSETS=all                                 #安裝所有的字符集
# -DMYSQL_UNIX_ADDR=/data/mysql/data/mysqld.sock       #連接數據庫socket路徑
# -DENABLED_LOCAL_INFILE=1                             #允許從本地導入數據
# -DWITH_MYISAM_STORAGE_ENGINE=1                       #支持MyIASM引擎
# -DWITH_INNOBASE_STORAGE_ENGINE=1                     #支持InnoDB引擎
# -DWITH_MEMORY_STORAGE_ENGINE=1                       #支持Memory引擎
# -DWITH_PARTITION_STORAGE_ENGINE=1                    #安裝支持數據庫分區
# -DWITH_READLINE=1                                    #快捷鍵功能
# -DMYSQL_TCP_PORT=3306                                #端口

[[email protected] mysql-5.7.18]# make -j `grep processor /proc/cpuinfo | wc -l`  && make install

# grep processor /proc/cpuinfo 根據cpu核心數指定編譯時的線程數(多核cpu會快很多)

初始化配置

[[email protected] ~]# chown -R mysql:mysql /data/mysql/data/
[[email protected] mysql-5.7.18]# /usr/local/mysql/bin/mysqld --initialize --user=mysql
2017-04-16T09:35:15.850060Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-04-16T09:35:16.331637Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-04-16T09:35:16.408336Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-04-16T09:35:16.467944Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ffbe4813-2287-11e7-9d8c-000c298c8776.
2017-04-16T09:35:16.469737Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2017-04-16T09:35:16.470947Z 1 [Note] A temporary password is generated for [email protected]: sRXga&j#w5n
[[email protected] mysql-5.7.18]# /usr/local/mysql/bin/mysql_ssl_rsa_setup  --user=mysql

配置文件

[[email protected] ~]# cp /etc/my.cnf /etc/my.cnf.bak 
[[email protected] ~]# rm -rf /etc/my.cnf
[[email protected] ~]# vim /etc/my.cnf

[client]
port = 3306
socket = /data/mysql/data/mysql.sock
default-character-set = utf8
 
[mysqld]
port = 3306
socket = /data/mysql/data/mysql.sock
 
basedir = /usr/local/mysql
datadir = /data/mysql/data
pid-file = /data/mysql/data/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
 
init-connect = ‘SET NAMES utf8‘
character-set-server = utf8
 
#skip-name-resolve
#skip-networking
back_log = 300
 
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
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 = 30
 
log_error = /data/mysql/data/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/data/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 = 16M
 
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
EOF

系統服務

[root@localhost ~]# cp /usr/local/src/mysql-5.7.18/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# chmod +x /etc/init.d/mysqld 
[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# chkconfig mysqld on
[root@localhost ~]# service mysqld start

方法二
[root@localhost ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=Mysql
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/data/mysql/data/mysql.pid
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false

[Install]

[root@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl enable mysqld.service 

[root@localhost ~]# mysqladmin -u root -p password   #重置密碼
Enter password: 
New password: 
Confirm new password

環境變量

[root@localhost ~]# vim /etc/profile
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
[root@localhost ~]# source /etc/profile

msyql多實例(完全獨立)

##準備數據目錄
[[email protected] local]# mkdir /data/mysql/{3306,3307}
[[email protected] local]# chown mysql:mysql /data/mysql/
[[email protected] local]# chown mysql:mysql /data/mysql/
[[email protected] local]# mv /usr/local/mysql /tmp
[[email protected] local]# cd /usr/local/src/mysql-5.7.18/
[[email protected] mysql-5.7.18]# make install
##初始化數據庫
[[email protected] mysql-5.7.18]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/3306
[[email protected] mysql-5.7.18]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/3307
##配置數據庫目錄
[[email protected] mysql-5.7.18]# mv /usr/local/mysql /usr/local/mysql3306
[[email protected] mysql-5.7.18]# make install
[[email protected] mysql-5.7.18]# mv /usr/local/mysql /usr/local/mysql3307
[[email protected] mysql-5.7.18]# cd /usr/local/
[[email protected] local]# cp /etc/my.cnf /usr/local/mysql3306
[[email protected] local]# cp /etc/my.cnf /usr/local/mysql3307
[[email protected] local]# mv /etc/my.cnf /etc/my.cnf.bak
##修改my.cnf文件中的路徑
[[email protected] local]# vim mysql3306/my.cnf
[client]  
port = 3306
socket = /data/mysql/3306/mysql.sock
default-character-set = utf8

[mysqld]
port = 3306
socket = /data/mysql/3306/mysql.sock

basedir = /usr/local/mysql3306
datadir = /data/mysql/3306
pid-file = /data/mysql/3306/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
[[email protected] local]# vim mysql3307/my.cnf
[client]
port = 3307
socket = /data/mysql/3307/mysql.sock
default-character-set = utf8

[mysqld]
port = 3307
socket = /data/mysql/3307/mysql.sock

basedir = /usr/local/mysql3307
datadir = /data/mysql/3307
pid-file = /data/mysql/3307/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 2
##配置mysql 啟動腳本
[[email protected] local]# vim mysql3306/support-files/mysql.server 
then
  basedir=/usr/local/mysql3306
  bindir=/usr/local/mysql3306/bin
  if test -z "$datadir"
  then
    datadir=/data/mysql/3306
  fi
  sbindir=/usr/local/mysql3306/bin
  libexecdir=/usr/local/mysql3306/bin

[[email protected] local]# vim mysql3307/support-files/mysql.server 
then
  basedir=/usr/local/mysql3307
  bindir=/usr/local/mysql3307/bin
  if test -z "$datadir"
  then
    datadir=/data/3307/data
  fi
  sbindir=/usr/local/mysql3307/bin
  libexecdir=/usr/local/mysql3307/bin

## 啟動服務

[[email protected] local]# /usr/local/mysql3306/support-files/mysql.server start
[[email protected] local]# /usr/local/mysql3307/support-files/mysql.server start
[[email protected] local]# netstat -anpt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      40724/mysqld        
tcp        0      0 0.0.0.0:3307            0.0.0.0:*               LISTEN      47249/mysqld 

mysqld_multi管理多實例

##環境準備
[[email protected] local]# mkdir /data/mysql/{3306,3307}
[[email protected] local]# chown mysql:mysql /data/mysql/
[[email protected] local]# cd /usr/local/src/mysql-5.7.18/
[[email protected] mysql-5.7.18]# make install
## 修改my.cnf
[[email protected] mysql-5.7.18]# vim /etc/my.cnf

[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
log = /data/mysql/mysqld_multi.log

[mysqld1]
port = 3306
socket = /data/mysql/3306/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql/3306
pid-file = /data/mysql/3306/mysql.pid
user = mysql
log_error = /data/mysql/3306/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/3306/mysql-slow.log
general_log=ON
general_log_file= /data/mysql/3306/mysql.log
performance_schema = 0
explicit_defaults_for_timestamp
[mysqld2]
port = 3307
socket = /data/mysql/3307/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql/3307
pid-file = /data/mysql/3307/mysql.pid
user = mysql
log_error = /data/mysql/3307/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/3307/mysql-slow.log
general_log=ON
general_log_file= /data/mysql/3307/mysql.log
performance_schema = 0
explicit_defaults_for_timestamp

## 初始化數據庫
# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/3306
# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/3307
## 服務管理
# /usr/local/mysql/bin/mysqld_multi report      #查看所有實例狀態
# /usr/local/mysql/bin/mysqld_multi start       #啟動所有實例
# /usr/local/mysql/bin/mysqld_multi start 1     #啟動實例1
# mysqladmin -uroot -prVrzzT9s -S /data/mysql/3306/mysql.sock shutdown  #關閉某個實例
## 設置密碼
[root@localhost ~]# mysqladmin -u root -p password -P 3306 -S /data/mysql/3306/mysql.sock
[root@localhost ~]# mysqladmin -u root -p password -P 3307 -S /data/mysql/3307/mysql.sock

yum && 編譯 安裝mysql 5.7 多實例