天天看點

MySQL 5.7 主從複制配置軟體準備主從節點部署 MySQL主從複制配置

軟體準備

  • mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz(下載下傳位址)
    MySQL 5.7 主從複制配置軟體準備主從節點部署 MySQL主從複制配置

主從節點部署 MySQL

  • 解壓 MySQL,建立目錄
[[email protected] ~]# cd /usr/local
[[email protected] local]# tar -xzvf mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz
[[email protected] local]# mv mysql-5.7.11-linux-glibc2.5-x86_64 mysql
[[email protected] local]# mkdir mysql/arch mysql/tmp
           
  • 建立 mysqladmin 使用者及使用者組
[[email protected] local]# groupadd -g 101 dba
[[email protected] local]# useradd -u 514 -g dba -G root -d /usr/local/mysql mysqladmin
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
[[email protected] local]# id mysqladmin
uid=514(mysqladmin) gid=101(dba) groups=101(dba),0(root)

# 此時切換到 mysqladmin 使用者會發現沒有正常顯示
# 退出該使用者然後拷貝環境變量的配置檔案到 mysqladmin 使用者的家目錄
[[email protected] local]# su - mysqladmin
-bash-4.2$ exit
logout
[[email protected] local]# cp /etc/skel/.* /usr/local/mysql
cp: omitting directory ‘/etc/skel/.’
cp: omitting directory ‘/etc/skel/..’
cp: omitting directory ‘/etc/skel/.mozilla’
[[email protected] local]# su - mysqladmin
Last login: Sat Dec 21 22:02:59 CST 2019 on pts/0
[[email protected] ~]$
           
  • 配置 mysqladmin 使用者的環境變量
[[email protected] local]# vi mysql/.bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

#env
export MYSQL_HOME=/usr/local/mysql
export PATH=${MYSQL_HOME}/bin:$PATH
           
  • 配置 mysql 配置檔案 my.cnf
[[email protected] local]# vi /etc/my.cnf

# 下面的可以直接拷貝使用

[client]
port            = 3306
socket          = /usr/local/mysql/data/mysql.sock
default-character-set=utf8mb4

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

skip-slave-start

skip-external-locking
key_buffer_size = 256M
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 4M
query_cache_size= 32M
max_allowed_packet = 16M
myisam_sort_buffer_size=128M
tmp_table_size=32M

table_open_cache = 512
thread_cache_size = 8
wait_timeout = 86400
interactive_timeout = 86400
max_connections = 600

# Try number of CPU's*2 for thread_concurrency
#thread_concurrency = 32 

#isolation level and default engine 
default-storage-engine = INNODB
transaction-isolation = READ-COMMITTED

server-id  = 1739
basedir     = /usr/local/mysql
datadir     = /usr/local/mysql/data
pid-file     = /usr/local/mysql/data/hostname.pid

#open performance schema
log-warnings
sysdate-is-now

# 一定要為行模式
binlog_format = ROW
log_bin_trust_function_creators=1
log-error  = /usr/local/mysql/data/hostname.err
log-bin = /usr/local/mysql/arch/mysql-bin
expire_logs_days = 7

innodb_write_io_threads=16

relay-log  = /usr/local/mysql/relay_log/relay-log
relay-log-index = /usr/local/mysql/relay_log/relay-log.index
relay_log_info_file= /usr/local/mysql/relay_log/relay-log.info

log_slave_updates=1
gtid_mode=OFF
enforce_gtid_consistency=OFF

# slave
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=4
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON

#other logs
#general_log =1
#general_log_file  = /usr/local/mysql/data/general_log.err
#slow_query_log=1
#slow_query_log_file=/usr/local/mysql/data/slow_log.err

#for replication slave
sync_binlog = 500

#for innodb options 
innodb_data_home_dir = /usr/local/mysql/data/
innodb_data_file_path = ibdata1:1G;ibdata2:1G:autoextend

innodb_log_group_home_dir = /usr/local/mysql/arch
innodb_log_files_in_group = 4
innodb_log_file_size = 1G
innodb_log_buffer_size = 200M

# 根據生産需要,調整 pool size 
innodb_buffer_pool_size = 2G
#innodb_additional_mem_pool_size = 50M #deprecated in 5.6
tmpdir = /usr/local/mysql/tmp

innodb_lock_wait_timeout = 1000
#innodb_thread_concurrency = 0
innodb_flush_log_at_trx_commit = 2

innodb_locks_unsafe_for_binlog=1

#innodb io features: add for mysql5.5.8
performance_schema
innodb_read_io_threads=4
innodb-write-io-threads=4
innodb-io-capacity=200
#purge threads change default(0) to 1 for purge
innodb_purge_threads=1
innodb_use_native_aio=on

#case-sensitive file names and separate tablespace
innodb_file_per_table = 1
lower_case_table_names=1

[mysqldump]
quick
max_allowed_packet = 128M

[mysql]
no-auto-rehash
default-character-set=utf8mb4

[mysqlhotcopy]
interactive-timeout

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
           
  • 修改 mysql 配置檔案和檔案夾的權限
[[email protected] local]# chown mysqladmin:dba /etc/my.cnf
[[email protected] local]# chmod 640 /etc/my.cnf
[[email protected] local]# chown -R mysqladmin:dba /usr/local/mysql
[[email protected] local]# chmod -R 750 /usr/local/mysql
           
  • 配置開機自起
# 将服務檔案拷貝到 init.d 目錄下,并重命名為 mysql
[[email protected] local]# cp mysql/support-files/mysql.server /etc/rc.d/init.d/mysql
[[email protected] local]# chmod +x /etc/rc.d/init.d/mysql
[[email protected] local]# chkconfig --del mysql
[[email protected] local]# chkconfig --add mysql
[[email protected] local]# chkconfig --level 345 mysql on
           
  • 執行安裝程式,等待安裝完成
[[email protected] local]# su - mysqladmin
Last login: Sat Dec 21 22:41:28 CST 2019 on pts/0
[[email protected] ~]$ bin/mysqld \
> --defaults-file=/etc/my.cnf \
> --user=mysqladmin \
> --basedir=/usr/local/mysql/ \
> --datadir=/usr/local/mysql/data/ \
> --initialize
           
  • 檢視生成的臨時密碼,啟動 MySQL 服務
# 檢視生成的臨時密碼
[[email protected] data]$ cat data/hostname.err | grep password
2019-12-21T14:43:21.346940Z 1 [Note] A temporary password is generated for [email protected]: g4awpI:eh.YS

# 啟動 mysql 服務
[[email protected] ~]$ bin/mysqld_safe --defaults-file=/etc/my.cnf &
[1] 5694
[[email protected] ~]$ 2019-12-21T14:49:31.688279Z mysqld_safe Logging to '/usr/local/mysql/data/hostname.err'.
2019-12-21T14:49:31.740739Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

[[email protected] ~]$ service mysql status
 SUCCESS! MySQL running (6511)
           
  • 進入指令行,修改臨時密碼
[[email protected] ~]$ mysql -uroot -p'g4awpI:eh.YS'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.11-log

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user [email protected] identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

# 使用新密碼重新登入
[[email protected] ~]$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 50
Server version: 5.7.11-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)
           
  • 從節點 MySQL 安裝注意事項
    • 從節點 MySQL 安裝步驟與主節點基本一緻,需要注意的是 my.cnf 檔案中的 server-id 不能重複
    • 從節點在 mysqladmin 家目錄下需要建立一個 relay_log 目錄,否則配置主從複制時會報錯

主從複制配置

  • 在主庫建立複制使用者,檢視位置資訊
mysql> grant replication slave on *.* to repluser@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      989 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
           
  • 在從庫配置主庫資訊以及同步主庫的位置
mysql> change master to
    -> master_host='192.168.146.100',
    -> master_port=3306,
    -> master_user='repluser',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=989;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;

主要關注下面兩個為 YES 即可
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
           

繼續閱讀