1.下載必要的rpm包
登錄mysql官網下載:https://dev.mysql.com/downloads/mysql/
我系統是centos 7 的所以選擇紅帽的操作系統
2.安裝mysql 先用sudo root運行,我這是直接su root切換了root用戶
yum remove mariadb-libs rpm -ivh mysql-community-common-8.0.20-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm rpm -ivh mysql-community-client-8.0.20-1.el7.x86_64.rpm rpm -ivh mysql-community-server-8.0.20-1.el7.x86_64.rpm
3.啟動mysql
service mysqld start
4.查看密碼
grep 'temporary password' /var/log/mysqld.log
5.修改密碼
mysql -uroot -p
輸入密碼
#Root_123456 是新密碼,如果出現
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
是因為密碼太簡單,要改成帶特殊字符的復雜密碼
alter user 'root'@'localhost' IDENTIFIED BY '#Root_123456';
修改成功
6:設置允許遠程登錄
use mysql; update user set host='%' where user = 'root';
然后重啟mysql
service mysqld restart
7.搭建集群
准備三台集群
修改hosts文件
vi /etc/hosts
192.168.10.11 linux1 192.168.10.12 linux2 192.168.10.13 linux3
設置免密
ssh-keygen -t rsa ssh-copy-id linux1 ssh-copy-id linux2 ssh-copy-id linux3
設置遠程登錄並且刷新
grant all privileges on *.* to 'root'@'%' with grant option; flush privileges;
安裝 mysqlsh
rpm -ivh mysql-shell-8.0.20-1.el7.x86_64.rpm
登錄linux2安裝mysql8
在linux2/linu3從linux1 拷貝所有rpm包到本地
scp -r linux1:/opt/software/ /opt/software/
然后安裝
然后用mysqlsh搭建
shell.connect('root@linux1:3306') dba.configureLocalInstance() shell.connect('root@linux2:3306') dba.configureLocalInstance() shell.connect('root@linux3:3306') dba.configureLocalInstance() shell.connect('root@linux1:3306') var cluster=dba.createCluster("MySQL_Cluster")
如果不想用root用戶,建議用root用戶
set sql_log_bin=0; create user rpl_user@'%' identified by '#Root_123456'; grant replication slave,replication client on *.* to rpl_user@'%'; create user rpl_user@'127.0.0.1' identified by '#Root_123456'; grant replication slave,replication client on *.* to rpl_user@'127.0.0.1'; create user rpl_user@'localhost' identified by '#Root_123456'; grant replication slave,replication client on *.* to rpl_user@'localhost'; set sql_log_bin=1; change master to master_user='rpl_user', master_password='#Root_123456' for channel 'group_replication_recovery'; install plugin group_replication soname 'group_replication.so'; set global group_replication_bootstrap_group=on; start group_replication; set global group_replication_bootstrap_group=off;
關閉防火牆
# 關閉防火牆 systemctl stop firewalld.service # 禁用防火牆 systemctl disable firewalld.service vi /etc/selinux/config SELINUX=disabled
安裝mysql-router
rpm -ivh mysql-router-community-8.0.20-1.el7.x86_64.rpm
vim /etc/mysqlrouter/mysqlrouter.conf
[DEFAULT] logging_folder = /var/log/mysqlrouter runtime_folder = /var/run/mysqlrouter config_folder = /etc/mysqlrouter [logger] level = INFO [routing:read_write] bind_address = 192.168.10.11 bind_port = 7001 mode = read-write destinations = linux1:3306,linux2:3306 protocol=classic max_connections=1024 [routing:read_only] bind_address = 192.168.10.11 bind_port = 7002 mode = read-only destinations = linux1:3306,linux2:3306 protocol=classic max_connections=1024 # If no plugin is configured which starts a service, keepalive # will make sure MySQL Router will not immediately exit. It is # safe to remove once Router is configured. [keepalive] interval = 60
重啟mysqlrouter
systemctl restart mysqlrouter