mysql叢集搭建
MySQL版本 mysql-5.7.28
系統 centos7.9
主庫 dbserver01 10.8.98.102
從庫1 dbserver02 10.8.98.103
從庫2 dbserver03 10.8.98.104
先安裝主庫,備庫複製即可
安裝主庫
1、 修改主機名,更改hosts檔案
hostnamectl set-hostname dbserver01 echo "xx.xx.xx.xxx dbserver01" >> /etc/hosts
2、 解除安裝主機自帶mysql
rpm -qa | grep *mysql* rpm -e --nodeps mysql rpm -qa | grep mariadb* rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
3、 安裝mysql5.7
mkdir -p /u01/app/ mkdir -p /u01/data/3306 mkdir -p /u01/log/3306 tar xvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.28-linux-glibc2.12-x86_64 /u01/app/mysql
4、建立mysql使用者組,目錄授權
groupadd mysql useradd -r -g mysql -s /bin/false mysql chown -R mysql:mysql /u01
vi ~/.bash_profile PATH=$PATH:/u01/app/mysql/bin:$HOME/bin
5、 配置引數檔案
[root@dbserver01 ~]# cat /u01/data/3306/my.cnf [mysql] default-character-set=utf8 socket=/u01/data/3306/mysql.sock [mysqld] #skip-name-resolve port = 3306 socket=/u01/data/3306/mysql.sock basedir=/u01/app/mysql datadir=/u01/data/3306/data character-set-server=utf8 default-storage-engine=INNODB innodb_buffer_pool_size = 200M max_allowed_packet=16M explicit_defaults_for_timestamp=1 log-output=FILE general_log = 0 general_log_file=/u01/log/3306/3306db-general.err slow_query_log = ON slow_query_log_file=/u01/log/3306/3306db-query.err long_query_time=10 log-error=/u01/log/3306/3306db-error.err [root@dbserver01 ~]#
6、mysql資料庫初始化
ln -sf /u01/data/3306/my.cnf /etc/my.cnf /u01/app/mysql/bin/mysqld --initialize --user=mysql --basedir=/u01/app/mysql/ --datadir=/u01/data/3306/data/
7 、配置CENTOS linux啟動和停止指令碼
vi /usr/lib/systemd/system/mysqld.service [Unit] Description=MySQL Server Documentation=man:mysqld(8) Documentation=http://dev/mysql/doc/refman/en/using-sysemd.html After=syslog.target After=network.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql ExecStart=/u01/app/mysql/bin/mysqld --defaults-file=/u01/data/3306/my.cnf LimitNOFILE = 65536 LimitNPROC = 65536
8、配置systemctl 啟動和停止指令碼方法
systemctl daemon-reload systemctl stop mysqld systemctl start mysqld systemctl enable mysqld systemctl status mysqld
手動啟動和停止mysql的方法
nohup /u01/app/mysql/bin/mysqld_safe --defaults-file=/u01/data/3306/my.cnf & mysqladmin -uroot -p shutdown -S /u01/data/3306/mysql.sock
9、修改root密碼
mysql -uroot -p
初始密碼可以在檔案/u01/log/3306/3306db-error.err中檢視
alter user 'root'@'localhost' IDENTIFIED BY 'rootroot';
複製主庫搭建備庫。
三臺主機hosts檔案主機名都改好
[root@dbserver01 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.8.98.102 dbserver01 10.8.98.103 dbserver02 10.8.98.104 dbserver03 解除安裝主機自帶mysql rpm -qa | grep *mysql* rpm -e --nodeps mysql rpm -qa | grep mariadb* rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64 建立mysql使用者組 groupadd mysql useradd -r -g mysql -s /bin/false mysql vi ~/.bash_profile PATH=$PATH:/u01/app/mysql/bin:$HOME/bin 建立軟連線 ln -sf /u01/data/3306/my.cnf /etc/my.cnf 執行上面 7、配置CENTOS linux啟動和停止指令碼 停止主庫並打包壓縮並複製到備機根目錄 [root@dbserver01 ~]# systemctl stop mysqld [root@dbserver01 ~]# tar zcvf mysql20211019.tar.gz /u01 [root@dbserver01 ~]# scp mysql20211019.tar.gz 10.8.98.103:/ [root@dbserver01 ~]# scp mysql20211019.tar.gz 10.8.98.104:/ 備機解壓 [root@dbserver02 /]# tar zxvf mysql20211019.tar.gz [root@dbserver03 /]# tar zxvf mysql20211019.tar.gz 配置systemctl 啟動和停止指令碼方法 systemctl daemon-reload systemctl stop mysqld systemctl start mysqld systemctl enable mysqld systemctl status mysqld
現在三臺機器都可以啟動mysqld了
刪除2臺備機auto.cnf檔案後重啟mysqld,因為是複製過來的uuid一樣需要重新生成
[root@dbserver03 data]# pwd /u01/data/3306/data [root@dbserver03 data]# rm -f auto.cnf [root@dbserver03 data]# systemctl restart mysqld
01.準備主從二進位制與中繼日誌目錄(三臺機器操作) mkdir -p /u01/log/3306/binlog mkdir -p /u01/log/3306/relaylog chown -R mysql:mysql /u01/log/3306/binlog chown -R mysql:mysql /u01/log/3306/relaylog chmod -R 775 /u01/log/3306/binlog chmod -R 775 /u01/log/3306/relaylog 02.準備主庫引數 #mysql cluster master add bind-address=10.8.98.102 server_id=1023306 skip_name_resolve=ON expire_logs_days=7 innodb_support_xa=1 binlog_cache_size=1M max_binlog_size=2048M log_bin_trust_function_creators=1 innodb_flush_log_at_trx_commit=1 sync_binlog=1 transaction-isolation=READ-COMMITTED gtid_mode=ON enforce_gtid_consistency=1 log-slave-updates=1 binlog_gtid_simple_recovery=1 log_bin=/u01/log/3306/binlog/dbserver-binlog log_bin_index=/u01/log/3306/binlog/dbserver-binlog.index binlog_format=ROW binlog_rows_query_log_events=on plugin_dir=/u01/app/mysql/lib/plugin/ plugin_load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" loose_rpl_semi_sync_master_enabled=1 loose_rpl_semi_sync_slave_enabled=1 loose_rpl_semi_sync_master_timeout=5000 rpl_semi_sync_master_wait_point=AFTER_SYNC rpl_semi_sync_master_wait_for_slave_count=1 03.準備從庫引數 #mysql cluster slave add bind-address=10.8.98.103 server_id=1033306 skip_name_resolve=ON expire_logs_days=7 innodb_support_xa=1 binlog_cache_size=1M max_binlog_size=2048M log_bin_trust_function_creators=1 innodb_flush_log_at_trx_commit=1 sync_binlog=1 transaction-isolation=READ-COMMITTED gtid_mode=ON enforce_gtid_consistency=1 log-slave-updates=1 binlog_gtid_simple_recovery=1 log_bin=/u01/log/3306/binlog/dbserver-binlog log_bin_index=/u01/log/3306/binlog/dbserver-binlog.index binlog_format=ROW binlog_rows_query_log_events=on relay_log=/u01/log/3306/relaylog/dbserver-relay.log read_only=1 plugin_dir=/u01/app/mysql/lib/plugin/ plugin_load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" loose_rpl_semi_sync_master_enabled=1 loose_rpl_semi_sync_slave_enabled=1 loose_rpl_semi_sync_master_timeout=5000 rpl_semi_sync_master_wait_point=AFTER_SYNC rpl_semi_sync_master_wait_for_slave_count=1 04.主從庫建立複製使用者並授權(三臺機器重啟mysqld後操作) mysql -uroot -prootroot create user 'repuser'@'%' identified with mysql_native_password by 'repuser123'; grant replication client,replication slave on *.* to 'repuser'@'%'; flush privileges; select user,host from mysql.user; 05.在主庫從庫使slave與master建立連線(記得關閉防火牆) 主庫操作 reset master; 從庫操作 stop slave; reset master; change master to master_host='10.8.98.102', master_port=3306, master_user='repuser', master_password='repuser123', master_auto_position=1; start slave; 注意:首次搭建主從,如果主機本身有資料了,就是說有了庫或者表,要先手工同步一把到備庫,然後再啟用主從同步
驗證:
主庫建立一個database test
mysql> show master status -> ; +------------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------------+----------+--------------+------------------+-------------------+ | dbserver-binlog.000001 | 154 | | | | +------------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> show processlist; +----+---------+-------------------+------+------------------+------+---------------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+---------+-------------------+------+------------------+------+---------------------------------------------------------------+------------------+ | 3 | repuser | 10.8.98.103:40960 | NULL | Binlog Dump GTID | 137 | Master has sent all binlog to slave; waiting for more updates | NULL | | 4 | repuser | 10.8.98.104:43560 | NULL | Binlog Dump GTID | 134 | Master has sent all binlog to slave; waiting for more updates | NULL | | 5 | root | localhost | NULL | Query | 0 | starting | show processlist | +----+---------+-------------------+------+------------------+------+---------------------------------------------------------------+------------------+ 3 rows in set (0.01 sec) mysql> create database test; Query OK, 1 row affected (0.00 sec) mysql>
備機:
mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.8.98.102 Master_User: repuser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: dbserver-binlog.000001 Read_Master_Log_Pos: 154 Relay_Log_File: dbserver-relay.000002 Relay_Log_Pos: 379 Relay_Master_Log_File: dbserver-binlog.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec)
搭建成功
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70004783/viewspace-2838198/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL MGR 叢集搭建MySql
- docker搭建mysql叢集DockerMySql
- MySQL 5.7 叢集搭建MySql
- mysql8叢集搭建MySql
- MySQL叢集搭建方案(PXC)MySql
- MySQL叢集搭建(1)-主備搭建MySql
- Linux中Mysql的叢集搭建LinuxMySql
- MySQL MGR 叢集搭建(單主模式)MySql模式
- 搭建 MySQL 高可用高效能叢集MySql
- 搭建zookeeper叢集(偽叢集)
- zookeeper叢集及kafka叢集搭建Kafka
- MySQL叢集搭建(6)-雙主+keepalived高可用MySql
- Docker Compose搭建MySQL主從複製叢集DockerMySql
- Redis系列:搭建Redis叢集(叢集模式)Redis模式
- linux下搭建ZooKeeper叢集(偽叢集)Linux
- Hadoop叢集搭建Hadoop
- Zookeeper叢集搭建
- redis叢集搭建Redis
- zookeeper 叢集搭建
- 搭建 Redis 叢集Redis
- RabbitMQ叢集搭建MQ
- nacos 叢集搭建
- HBASE叢集搭建
- 搭建ELK叢集
- Ambari叢集搭建
- kafka叢集搭建Kafka
- Hadoop搭建叢集Hadoop
- 【環境搭建】RocketMQ叢集搭建MQ
- Docker Swarm 叢集搭建DockerSwarm
- 搭建redis cluster叢集Redis
- Hbase 2.2.5叢集搭建
- docker搭建consul叢集Docker
- 快速搭建Jenkins叢集Jenkins
- 搭建MongoDB分片叢集MongoDB
- redis 4.0.11 叢集搭建Redis
- 【greenplum】greenplum叢集搭建
- Kubernetes 叢集搭建(上)
- Kubernetes 叢集搭建(下)