版本:MySQL-5.7.20 mysql
1、《編譯安裝》sql
一、更換yum源bootstrap
wget -O /etc/yum.repos.d/eprl.repo http://mirrors.aliyun.com/repo/epel-6.repovim
二、安裝cmakebash
tar xf cmake-3.0.0.tar.gz網絡
cd cmake-3.0.0app
./bootstrapsocket
makeide
make install測試
二、安裝boost
tar -zxvf boost_1_59_0.tar.gz -C /usr/local/
三、建立安裝目錄及所須要文件
mkdir -p /application/mysql-5.7.22/{data,logs,pids}
touch /application/mysql-5.7.22/logs/mysqld.log
touch /application/mysql-5.7.22/pids/mysqld.pid
四、安裝依賴軟件包
yum -y install ncurses ncurses-devel bison libgcrypt perl
五、建立用戶
useradd mysql -s /sbin/nologin -M
六、解壓並安裝
tar xf mysql-5.7.22.tar.gz
cd mysql-5.7.22
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.7.22 -DMYSQL_DATADIR=/application/mysql-5.7.22/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DENABLE_DOWNLOADS=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
make
make install
七、建立軟鏈接
ln -s /application/mysql-5.7.22 /application/mysql
八、添加環境變量
echo "PATH=/application/mysql/bin:/application/mysql/lib:$PATH" >>/etc/profile
source /etc/profile
九、初始化
mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
十、複製啓動命令
/bin/cp support-files/mysql.server /etc/init.d/mysqld
十一、修改/etc/my.cnf
[mysqld]
datadir=/application/mysql/data
socket=/application/mysql/mysql.sock
[mysqld_safe]
log-error=/application/mysql/logs/mysqld.log
pid-file=/application/mysql/pids/mysqld.pid
symbolic-links=0
[client]
default-character-set=utf8
socket=/application/mysql/mysql.sock
[mysql]
default-character-set=utf8
socket=/application/mysql/mysql.sock
十二、修改mysql安裝目錄的屬主
chown -R mysql.mysql /application/mysql
1三、啓動
/etc/init.a/mysqld start
1四、修改密碼
mysqladmin -uroot -S /application/mysql/mysql.sock password '123456'
2、《配置多實例》
一、關閉mysql
/etc/init.d/mysqld stop
二、建立所所須要目錄及文件
mkdir -p /data/{3306,3307}/data
touch /data/{3306,3307}/mysql.pid
touch /data/{3306,3307}/error.log
chown -R mysql.mysql /data/
三、初始化
mysqld --no-defaults --initialize-insecure --basedir=/application/mysql-5.7.22 --datadir=/data/3306/data/ --user=mysql --explicit_defaults_for_timestamp
四、分配獨立配置文件
#配置文件優先級,/etc/與basedir目錄下不要存在my.cnf配置文件
/etc/my.cnf >basedir/my.cnf >datadir/my.cnf
vim /data/3306/my.cnf
#可自由選擇,參數來自網絡
[mysqld]
port= 3306
socket= /data/3306/mysql.sock
datadir= /data/3306/data
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 1M
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 8K
read_buffer_size = 768K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
query_cache_size = 16M
tmp_table_size = 32M
log-error= /data/3306/error.log
pid-file= /data/3306/mysql.pid
explicit_defaults_for_timestamp = true
max_connections = 100
max_connect_errors = 5
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_data_home_dir = /data/3306/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /data/3306/data
innodb_buffer_pool_size = 128M
innodb_log_file_size = 32M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
五、啓動測試
mysqld_safe --defaults-file=/data/3306/my.cnf
六、mysql啓動腳本
#!/bin/bash
port=3306
mysql_user="root"
mysql_pwd="$2"
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
function start () {
if [ ! -e "${mysql_sock}" ];then
printf "Starting MySQL...\n"
/bin/bash ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 >/dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
function stop () {
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stoping...\n"
exit
else
printf "Stoping MySQL..."
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
fi
}
function restart () {
printf "Restarting MySQL...\n"
stop
sleep 2
start
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
printf "Usage: /data/${port}/mysql{start|stop|restart}\n"
esac
$*
七、啓動mysql
chmod +x mysql
./mysql start 123456
3、《主從複製》
Master
一、修改主庫配置文件在[mysqld]模塊下添加
log-bin=/data/3306/mysql-bin
server-id = 1
二、登陸查看顯示爲ON則配置成功
show variables like 'log_bin';
show variables like 'server_id';
三、建立賬號
從庫要想和主庫同步,必須有一個能夠鏈接主庫的賬號而且這個賬號是主庫上建立的,權限是容許主庫的從庫鏈接並同步數據。
grant replication slave on *.* to 'cst'@'%' identified by '123456';
flush privileges;
四、查看新建用戶與權限
select user,host from mysql.user;
show grants for cst@'%';
五、鎖主庫只讀
flush table with read lock;
六、默認狀況下自動解鎖的時長參數值爲
show variables like'%timeout%';
七、查看master狀態,並記錄以下兩行參數
show master status\G
File: mysql-bin.000001
Position: 588
八、備份主庫
mkdir -p /server/backup
mysqldump -uroot -p123456 -S mysql.sock --events -A -B | gzip >/server/backup/mysql_bak.$(date +%F).sql.gz
九、解鎖
unlock tables;
十、將主庫的數據發送到從庫/opt目錄下
scp /server/backup/mysql_bak.2018-05-01.sql.gz root@192.168.59.105:/opt/
Slave
一、修改配置文件
server-id = 3
二、解壓主庫的備份文件
gzip -d /opt/mysql_bak.2018-05-01.sql.gz
三、導入到從庫
mysql -uroot -p -S mysql.sock </opt/mysql_bak.2018-05-01.sql
注意:若是備份時使用了-A參數,則在還原數據到從庫時登陸密碼會和主庫一致。
四、登陸從庫,配置複製參數(./data/master.info文件)
CHANGE MASTER TO
master_host='192.168.59.104',
master_port=3306,
master_user='cst',
master_password='123456',
master_log_file='mysql-bin.000001',
master_log_pos=588;
五、啓動從庫開關
mysql -uroot -p -S mysql.sock -e "start slave;"
六、查看從庫狀態
mysql -uroot -p -S mysql.sock -e "show slave status\G;"
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
ps:有上面三例則成功
七、在Master建立一個新庫,觀查從庫狀態
create database cst;