MySQL主从(一):基本安装部署

需要部署一主一从,前面放置中间件kingshard,配置中间件之前,先配置好基础的主从环境

类目 属性 说明
系统 CentOS Linux release 7.2.1511 (Core)
ip-master 10.11.10.215 主数据库
ip-slave 10.11.10.216 从数据库
MySQL版本 5.7.20
同步模式 gtid

- 下载

[root@localhost ~]# mkdir -pv /data/download
[root@localhost ~]# cd /data/download/
[root@gzsd-mysql01-101110215 download]# wget "https://www.percona.com/downloads/Percona-Server-LATEST/Percona-Server-5.7.19-17/binary/tarball/Percona-Server-5.7.19-17-Linux.x86_64.ssl101.tar.gz"
[root@localhost mysql]# yum install libaio vim

采用的percona分支版本的考虑在于使用xtrabackup备份的时候,备份锁的粒度很小,而且有对应的线程池的功能。性能上比MySQL原本较为靠上(官方版本跟Percona性能测试不在今天这个范围之内)。
具体的配置过程

1.解压缩
[root@gzsd-mysql01-101110215 download]# tar xvf Percona-Server-5.7.19-17-Linux.x86_64.ssl101.tar.gz -C /usr/local/
2.新增用户
[root@gzsd-mysql01-101110215 download]# groupadd mysql    
[root@gzsd-mysql01-101110215 download]# useradd -r -g mysql -s /bin/false mysql
[root@gzsd-mysql01-101110215 download]# cd /usr/local/
[root@gzsd-mysql01-101110215 local]# ln -s Percona-Server-5.7.19-17-Linux.x86_64.ssl101 mysql
[root@gzsd-mysql01-101110215 local]# mkdir -pv /data/mysql/mysql3306/{data,tmp,logs/iblog}
[root@gzsd-mysql01-101110215 local]# chown -R mysql.mysql /data/mysql
[root@gzsd-mysql01-101110215 local]# cd /usr/local/mysql/
3.修改my.cnf文件
见文章末尾

4.初始化安装
[root@gzsd-mysql01-101110215 mysql]# ./bin/mysqld --initialize --user=mysql
#此时看error.log可以看到对应的的信息
[root@gzsd-mysql01-101110215 ~]# tailf /data/mysql/mysql3306/logs/error.log 
2017-11-27T15:16:42.919983+08:00 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
 100 200 300 400 500 600 700 800 900 1000
 100 200 300 400 500 600 700 800 900 1000
2017-11-27T15:16:58.822273+08:00 0 [Warning] InnoDB: New log files created, LSN=48433
2017-11-27T15:16:59.227121+08:00 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-11-27T15:16:59.905396+08:00 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: f58d0e35-d342-11e7-a0c8-525400c86529.
2017-11-27T15:16:59.963532+08:00 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-11-27T15:17:00.288153+08:00 0 [Warning] CA certificate ca.pem is self signed.
**2017-11-27T15:17:00.305884+08:00 1 [Note] A temporary password is generated for root@localhost: N%DZ&21KgNxp**


2017-11-27T15:17:21.693274+08:00 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2017-11-27T15:17:21.693300+08:00 1 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
2017-11-27T15:17:21.693308+08:00 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-11-27T15:17:21.693318+08:00 1 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
2017-11-27T15:17:21.693322+08:00 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-11-27T15:17:21.693328+08:00 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2017-11-27T15:17:21.693347+08:00 1 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
2017-11-27T15:17:21.693353+08:00 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.

加强的为默认生成的随机密码,首次登陆后需要进行修改,复制启动脚本到/etc/init.d中

[root@gzsd-mysql01-101110215 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@gzsd-mysql01-101110215 mysql]# chmod +x /etc/init.d/mysqld
[root@gzsd-mysql01-101110215 mysql]# cp /usr/local/Percona-Server-5.7.19-17-Linux.x86_64.ssl101/lib/mysql/libjemalloc.so /usr/lib64/
[root@gzsd-mysql01-101110215 mysql]# vim /etc/init.d/mysqld 
basedir=/usr/local/mysql
datadir=/data/mysql/mysql3306/data
#启动
[root@gzsd-mysql01-101110215 mysql]# /etc/init.d/mysqld start

如果报错,请查看/data/mysql/mysql3306/logs/error.log
顺利启动之后,首先需要进行一个安全加固,加默认密码进行更改

[root@localhost mysql]# ln -s /data/mysql/mysql3306/tmp/mysql.sock /tmp/mysql.sock

[root@gzsd-mysql01-101110215 mysql]# bin/mysql_secure_installation -S /data/mysql/mysql3306/tmp/mysql.sock
mysql_secure_installation: [ERROR] unknown variable 'default-character-set=utf8'

Securing the MySQL server deployment.

Enter password for user root: 输入初始化密码

The existing password for the user account root has expired. Please set a new password.

New password: 输入新密码

Re-enter new password: 确认密码

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y 是否使用增强的密码策略规则

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: 重新输入新密码

Re-enter new password: 确认密码

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y 是否移除匿名用户
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y 禁止root远程登录
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y 移除test数据库
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y 刷新权限列表
Success.

All done! 

将bin的路径添加到系统中

[root@gzsd-mysql01-101110215 mysql]# vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
[root@gzsd-mysql01-101110215 mysql]# source /etc/profile

至此,主库生成完毕,读库配置基本类同,不在累述,配置好读库后,进行主从配置

  • 主库配置
root@(none) 04:07:21>show global variables like "%server%";
+---------------------------------+--------------------------------------+
| Variable_name                   | Value                                |
+---------------------------------+--------------------------------------+
| character_set_server            | utf8mb4                              |
| collation_server                | utf8mb4_general_ci                   |
| innodb_ft_server_stopword_table |                                      |
| server_id                       | 330610216                            |
| server_id_bits                  | 32                                   |
| server_uuid                     | b9cff64e-d348-11e7-913c-525400f96d7f |
+---------------------------------+--------------------------------------+
6 rows in set (0.00 sec)

root@(none) 04:07:28>show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql_bin.000003 |      194 |              |                  | b9cff64e-d348-11e7-913c-525400f96d7f:1-4 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
root@(none) 04:17:25>GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl-oms-account'@'10.11.10.216' IDENTIFIED BY 'Hg;v13cB77]WNx';
Query OK, 0 rows affected, 1 warning (0.35 sec)

root@(none) 04:17:42>flush privileges;
Query OK, 0 rows affected (0.02 sec)

可以看到主库执行到binlog的位置,对主库进行备份

[root@gzsd-mysql01-101110215 download]# mysqldump -uroot -p --single-transaction --master-data=2 -A >backup.sql
[root@gzsd-mysql01-101110215 download]# scp backup.sql 10.11.10.216:/data/download/
  • 读库配置
[root@localhost mysql]# cd /data/download/
[root@localhost download]# mysql -uroot -p -S /data/mysql/mysql3306/tmp/mysql.sock < backup.sql 
[root@localhost download]# mysql -uroot -p -S /data/mysql/mysql3306/tmp/mysql.sock < backup.sql 
Enter password: 
ERROR 1840 (HY000) at line 33: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
[root@localhost download]# mysql -uroot -p -S /data/mysql/mysql3306/tmp/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.19-17-log Percona Server (GPL), Release 17, Revision e19a6b7b73f

Copyright (c) 2009-2017 Percona LLC and/or its affiliates
Copyright (c) 2000, 2017, 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.

root@(none) 04:22:07>reset master;
Query OK, 0 rows affected (0.13 sec)
[root@localhost download]# mysql -uroot -p -S /data/mysql/mysql3306/tmp/mysql.sock < backup.sql 
Enter password: 

重新执行就不会报错了。

root@(none) 04:24:07>change master to master_host="10.11.10.215",master_port=3306,master_user='repl-oms-account',master_password='Hg;v13cB77]WNx',master_auto_position=1;
root@(none) 04:26:03>start slave;

查看读库状态即可获知是否同步成功

root@(none) 04:27:00>show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.11.10.215
                  Master_User: repl-oms-account
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000003
          Read_Master_Log_Pos: 672
               Relay_Log_File: relaylog.000002
                Relay_Log_Pos: 414
        Relay_Master_Log_File: mysql_bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 672
              Relay_Log_Space: 614
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 330610215
                  Master_UUID: f58d0e35-d342-11e7-a0c8-525400c86529
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: f58d0e35-d342-11e7-a0c8-525400c86529:1-6
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

至此,基本的主从配置完毕。附录为my.cnf配置文件

[client]
port=3306
default-character-set=utf8
socket=/data/mysql/mysql3306/tmp/mysql.sock

[mysql]
port=3306
prompt=\\u@\\d \\r:\\m:\\s>
default-character-set=utf8
no-auto-rehash
socket=/data/mysql/mysql3306/tmp/mysql.sock

[mysqld]
port=3306
sql_mode='NO_ENGINE_SUBSTITUTION'
character-set-server=utf8mb4
default-storage-engine=INNODB
default-time-zone="+08:00"
log_timestamps=SYSTEM
explicit_defaults_for_timestamp=1
transaction-isolation = READ-COMMITTED
# optimizer_switch='mrr_cost_based=off,batched_key_access=on'
# directionary
innodb_log_group_home_dir=/data/mysql/mysql3306/data
innodb_data_home_dir=/data/mysql/mysql3306/data
innodb_data_file_path=ibdata1:10M:autoextend:max:10G
datadir=/data/mysql/mysql3306/data
tmpdir=/data/mysql/mysql3306/tmp/
slave_load_tmpdir=/data/mysql/mysql3306/tmp/
log_error=/data/mysql/mysql3306/logs/error.log
slow_query_log_file=/data/mysql/mysql3306/logs/slow.log
socket=/data/mysql/mysql3306/tmp/mysql.sock
log_bin=/data/mysql/mysql3306/logs/mysql_bin
relay_log=/data/mysql/mysql3306/logs/relaylog

# undo settings
innodb_undo_tablespaces=3
innodb_undo_logs=128
innodb_undo_directory=/data/mysql/mysql3306/logs/iblog
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 2G
innodb_purge_rseg_truncate_frequency = 128

# innodb settings
innodb_log_files_in_group=2
innodb_log_file_size=1G
innodb_flush_log_at_trx_commit=1
innodb_max_dirty_pages_pct=50
innodb_read_io_threads=8
innodb_write_io_threads=16
innodb_file_per_table=1
innodb_thread_concurrency=16
innodb_change_buffering=inserts
innodb_checksum_algorithm=crc32
innodb_adaptive_flushing=1
innodb_stats_on_metadata=0
innodb_flush_method=O_DIRECT
innodb_log_buffer_size=10M
innodb_flush_neighbors=0
innodb_lock_wait_timeout=50
innodb_sync_array_size=16
transaction-isolation = READ-COMMITTED
innodb_open_files=65535
open_files_limit=65535

# table
table_definition_cache=8192
table_open_cache=8192
table_open_cache_instances=16

# thread and thread pool
thread_stack=262144
thread_cache_size=256
thread_handling=pool-of-threads
thread_pool_high_prio_mode=transactions
thread_pool_idle_timeout=60
thread_pool_oversubscribe=8
thread_pool_size=32
thread_pool_stall_limit=10

# binlog and replication
binlog_cache_size=5M
binlog-format=ROW
sync_binlog=1
expire_logs_days=7
log_slave_updates = 1
relay_log_info_repository=TABLE
master_info_repository=TABLE
relay_log_recovery=1
max_binlog_size=512M
binlog-format=ROW
slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'

# semi sync replication settings
plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 300
rpl_semi_sync_slave_enabled = 1

# new replication setting
slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers = 16
slave_preserve_commit_order=1
slave_transaction_retries=128

# gtid
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_gtid_simple_recovery = 1

# slow log
log_slow_verbosity=full
long_query_time=1
slow_query_log=1
skip-slave-start=1

#coms
query_cache_type=0
query_cache_size=0
local-infile=1
max_allowed_packet=64M
read_rnd_buffer_size=16M
join_buffer_size=8M
sort_buffer_size=2M
read_buffer_size=4M
open_files_limit=65535
lower_case_table_names=1
skip-name-resolve=1
skip-external-locking=1
safe-user-create

#timeout
connect_timeout=30
delayed_insert_timeout =300
innodb_rollback_on_timeout=OFF
net_read_timeout=30
net_write_timeout=60
slave_net_timeout=30

#myisam
concurrent_insert=2
key_buffer_size=8M
myisam_sort_buffer_size=20M

# START CONFIGURE
server_id=330610215
max_connections=12000
max_user_connections=10000
max_connect_errors=35000
innodb_buffer_pool_size=2G
innodb_io_capacity=100
innodb_io_capacity_max=400
# END CONFIGURE

[mysqld_safe]
pid_file=/data/mysql/mysql3306/tmp/mysql.pid
malloc-lib=/usr/lib64/libjemalloc.so
numa-interleave=1

你可能感兴趣的:(MySQL)