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