1. 环境准备【GTID模式 mysql集群搭建】1.)修改主机名
[root@MySQL-master ~]#hostnamectl set-hostname mysql-master[root@mysql-master ~]#hostnamectl set-hostname mysql-slave01[root@mysql-master ~]#hostnamectl set-hostname mysql-slave02
2.)修改hosts文件
[root@mysql-master ~]#vim /etc/hosts192.168.10.231 mysql-master192.168.10.232 mysql-slave01192.168.10.233 mysql-slave02
3.)系统准备
[root@mysql-master ~]# yum -y install wget vim lrzsz net-tools ntp #修改yum源[root@mysql-master ~]# mv /etc/yum.repos.d/centos-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak[root@mysql-master ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo [root@mysql-master ~]# yum clean all[root@mysql-master ~]# yum -y update[root@mysql-master ~]# yum -y install gcc# 关闭 防火墙[root@mysql-master ~]# systemctl stop firewalld[root@mysql-master ~]# systemctl disable firewalld# 关闭 Selinux[root@mysql-master ~]# setenforce 0[root@mysql-master ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
3.)准备目录【将最大的磁盘挂载到/data目录】
[root@mysql-master ~]# mkdir /data/mysql -p[root@mysql-master ~]# mkdir /data/log/mysql/ -pv
2.mysql5.7安装1). 安装[root@mysql-master ~]# yum remove mysql-libs[root@mysql-master ~]# tar -xfmysql-5.7.25-1.el7.x86_64.rpm-bundle.tar[root@mysql-master ~]# rpm -ivh mysql-community-common-5.7.25-1.el7.x86_64.rpm mysql-community-libs-5.7.25-1.el7.x86_64.rpm mysql-community-client-5.7.25-1.el7.x86_64.rpm mysql-community-server-5.7.25-1.el7.x86_64.rpm mysql-community-devel-5.7.25-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm[root@mysql-master ~]# chown -R mysql:mysql /data/mysql[root@mysql-master ~]# chown -R mysql:mysql /data/log/
2). 替换配置文件/etc/my.cnf备注:从库请将server-id修改为大于1的整数 。
[mysqld]port = 3306server-id = 1datadir = /data/mysqlsocket = /dev/shm/mysql.sockpid-file = /var/run/mysqld/mysqld.pidtmpdir = /dev/shminnodb_buffer_pool_size = 1228Msort_buffer_size = 16Minnodb_sort_buffer_size = 32Mjoin_buffer_size = 32Mkey_buffer_size = 128Mread_buffer_size = 16Mread_rnd_buffer_size = 16Mbulk_insert_buffer_size = 64Mquery_cache_type = 1query_cache_size = 512Mquery_cache_limit = 16Mquery_cache_min_res_unit = 2kthread_stack = 256Ktmp_table_size = 256Mmax_heap_table_size = 256Mmax_allowed_packet = 64Minnodb_flush_method = O_DIRECTinnodb_flush_log_at_trx_commit = 2binlog-format = ROW# loglog-bin = mysql-master-binexpire_logs_days = 7slow_query_log = onlog-error = /data/log/mysql/mysqld.loglong_query_time = 2log-queries-not-using-indexes = 1slow_query_log_file = /data/log/mysql/mysqld01_slowquery.log#GTIDgtid-mode = onenforce_gtid_consistency = 1master-info-repository = TABLErelay-log-info-repository = TABLErelay_log_recovery = onsync-master-info = 1#othermax_connections = 1000table_open_cache = 4096skip_name_resolve = 1lower_case_table_names = 1log_bin_trust_function_creators = 1#modesql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES#skip-grant-tables
3).设置开机自动启动[root@mysql-master ~]# systemctl enable mysqld.service[root@mysql-master ~]# systemctl start mysqld.service
4). 首次root登录[root@mysql-master ~]# cat /data/log/mysql/mysqld.log|grep generated[Note] A temporary password is generated for root@localhost: xxI07yii>mJt#冒号后面后数据库的初始化密码
5). 给root修改密码[root@mysql-master ~]# mysql -S /dev/shm/mysql.sock -uroot -p#方法一mysql> SET PASSWORD = PASSWORD('Mayi123@');mysql> flush privileges;mysql> exit;#方法二:mysql> update user set authentication_string=password('Mayi123@') where user='root' and host='localhost';mysql> flush privileges;mysql> exit;#方法三mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mayi123@' PASSWORD EXPIRE NEVER;mysql> flush privileges;mysql> exit;
6). 新密码再次登录[root@mysql-master ~]# mysql -S /dev/shm/mysql.sock -uroot -pMayi123@
3.主从设置1).创建用于主从同步的账户#主库执行[root@mysql-master ~]# mysql -S /dev/shm/mysql.sock -uroot -pMayi123@mysql> grant replication slave on *.* to 'slave'@'192.168.%.%' identified by 'Slave@0000';mysql> flush privileges;mysql> show master logs;+-------------------------+-----------+| Log_name | File_size |+-------------------------+-----------+| mysql-master-bin.000001 | 177 || mysql-master-bin.000002 | 995 |+-------------------------+-----------+mysql> show binlog events in 'mysql-master-bin.000002'; #查看二进制日志中的执行命令
2).从库同步[root@mysql-slave01 ~]# mysql -S /dev/shm/mysql.sock -uroot -pMayi123@mysql> reset slave;mysql> stop slave;mysql> change master to master_host='192.168.10.231',master_port=3306,master_user='slave',master_password='Slave@0000',master_auto_position = 1;mysql> start slave;mysql> show slave status G;*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.231 Master_User: sync Master_Port: 4980 Connect_Retry: 60 Master_Log_File: mysql-master-bin.000002 Read_Master_Log_Pos: 995 Relay_Log_File: mysql-slave2-relay-bin.000002 Relay_Log_Pos: 1222 Relay_Master_Log_File: mysql-master-bin.000002 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: 995 Relay_Log_Space: 1436 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: 0Master_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: 1 Master_UUID: 95a3f56b-b6e4-11eb-8a6a-3a4c58bd3f13 Master_Info_File: /data/mysql/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: 95a3f56b-b6e4-11eb-8a6a-3a4c58bd3f13:1-4 Executed_Gtid_Set: 95a3f56b-b6e4-11eb-8a6a-3a4c58bd3f13:1-4,a00b9b71-b6e4-11eb-8a2f-f6cc164fe846:1-2 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
推荐阅读
- 监控mysql主从同步状态是否异常
- SQL优化最干货总结 - MySQL
- 淘宝的数据库,主键是如何设计的?
- Mysql注入 -- 联合注入
- Explain关键字 什么是MySQL的执行计划?
- Mysql删除数据表的三种方式详解
- 保税仓跨境电商?跨境电商进口保税模式下进入保税仓库的货物
- 看这篇就够了!MySQL 索引知识点超全总结
- 一次解决mysql锁表问题的经历
- CentOS 如何用rpm安装Mysql