eleme

设置主从从mysql57服务器

--配置主数据库

# systemctl stop firewalld
# setenforce 0
# systemctl disable firewalld
# ls
anaconda-ks.cfg mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz

# tar -xf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
# cp -r mysql-5.7.44-linux-glibc2.12-x86_64 /usr/local/mysql
# rm -rf /etc/my.cnf
# mkdir /usr/local/mysql/mysql-files
# useradd -r -s /sbin/nologin mysql
# chown mysql:mysql /usr/local/mysql/mysql-files
# chown 750 /usr/local/mysql/mysql-files
#初始化
# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql57

#修改配置文件
# vim /usr/local/mysql/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306
log-error=/usr/local/mysql/data/db01-master.err
log-bin=/usr/local/mysql/data/binlog
server-id=10
character_set_server=utf8mb4

#启动服务
# service mysql57 start

#再创建一个用户并且赋予权限

不能在从服务器

添加数据库和表

--配置从数据库

# tar -xf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
# cp -r mysql-5.7.44-linux-glibc2.12-x86_64 /usr/local/mysql
# rm -rf /etc/my.cnf
# mkdir /usr/local/mysql/mysql-files
# useradd -r -s /sbin/nologin mysql
# chown mysql:mysql /usr/local/mysql/mysql-files
# chown 750 /usr/local/mysql/mysql-files

#下载rsync
# yum -y install rsync

#同步data,停止master的服务
# service mysql57 stop
# rm -rf /usr/local/mysql/data/auto.cnf
# yum -y install rsync
# rsync -av /usr/local/mysql/data root@192.168.1.13:/usr/local/mysql/
# rsync -av /usr/local/mysql/data root@192.168.1.14:/usr/local/mysql/

--修改从数据库文件,两台从服务器server_id不能相同

# vim /usr/local/mysql/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306
log-error=/usr/local/mysql/data/err.log
relay-log=/usr/local/mysql/data/relaylog
character_set_server=utf8mb4
server-id=11

--主服务器锁表

mysql> select user,host from mysql.user;
mysql> flush tables with read lock;
Query OK, 0 rows a
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 | 154 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

--主从从搭建,两台同样操作

mysql> change master to
master_host="192.168.1.12",master_user="slave",master_password="slave",master_log_file="binlog.000002",
master_log_pos=154;
Query OK, 0 rows a
mysql> show slave status\G;
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

开放mysql服务端口、查看是否开启端口或者服务

添加登录数据,可以输入sql语句,也可以在图形化界面

添加mycat读写分离负载均衡

 nginx,haproxy,lvs负载均衡技术

 分不清什么数据库能读,什么数据库能写

 master mysql服务器是可以读写,slave mysql服务器是只读

在mysql中添加eleme数据库设置为utf8mb4

#在mysql中添加eleme数据库设置为utf8mb4
mysql> unlock tables;
mysql> create database if not exists eleme charset utf8mb4;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| eleme |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
6 rows in set (0.00 sec)
#添加表t_user(master)
mysql> use eleme;
mysql> create table t_user(
-> id int primary key auto_increment,
-> name varchar(45) not null,
-> username varchar(45) not null,
-> password varchar(45) not null,
-> remark varchar(45)
-> );
mysql> show tables;
+-----------------+
| Tables_in_eleme |
+-----------------+
| t_user |
+-----------------+
1 row in set (0.00 sec)
#添加2行记录(master)
mysql> insert into t_user (id,name,username,password,remark)values(1,"超级管理员","adm"in","admin","超级
管理员");
mysql> insert into t_user (id,name,username,password,remark)values(2,"普通用户","guest,","guest","普通用
户");

--使用mycat为三台数据库设置负载均衡(读写分 离)

# ls
anaconda-ks.cfg Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
jdk-8u192-linux-x64.tar.gz
# tar -xf jdk-8u192-linux-x64.tar.gz
# tar -xf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
# cp -r jdk1.8.0_192/ /usr/local/jdk
# cp -r mycat/ /usr/local/mycat
# sed -i '$aexport JAVA_HOME=/usr/local/jdk' /etc/profile
# source /etc/profile
# sed -i '$aexport PATH=$PATH:$JAVA_HOME/bin' /etc/profile
# source /etc/profile
# javac -version
javac 1.8.0_192
# /usr/local/mycat/bin/mycat console

#修改server.xml配置文件
# vim /usr/local/mycat/conf/server.xml
93   <user name="fubao" defaultAccount="true">
94 <property name="password">fubao</property>
95 <property name="schemas">eleme</property>
#注释掉108-112
#修改schema.xml 配置文件
5 <schema name="eleme" dataNode="dn1" checkSQLschema="false" sqlMaxLimit="10 0">
6 </schema>
20 <writeHost host="hostM1" url="192.168.2.57:3306" user="fubao"
21 password="fubao">
22 <!-- can have multi read hosts --> 23 <readHost host="hostS2"
url="192.168.2.58:3306" user="fubao " password="fubao" /> 24 <readHost host="hostS3"
url="192.168.2.59:3306" user="fubao " password="fubao" />
#启动服务
# /usr/local/mycat/bin/mycat start
# netstat -lntup | grep 8066
tcp6 0 0 :::8066 :::* LISTEN 1916/j

 mycat是阿里开源项目,所以必须安装java环境

jdk8u...    mycat1.6...

开发8066端口,上传安装包

解压

部署java环境

[root@java01 ~]# ls
anaconda-ks.cfg jdk-17_linux-x64_bin.tar.gz
[root@java01 ~]# tar -xf jdk-17_linux-x64_bin.tar.gz
[root@java01 ~]# ls
anaconda-ks.cfg jdk-17.0.12 jdk-17_linux-x64_bin.tar.gz
[root@java01 ~]# cp -r jdk-17.0.12/ /usr/local/jdk
[root@java01 ~]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export PATH=$PATH:$JAVA_HOME/bin
# source /etc/profile
# javac --version
javac 17.0.12
# vim application.yml
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.12:8066/eleme
username: fubao
password: fubao

上传好之后,连接mycat

vim application.yml

修改端口号、账号、密码

port默认是8080,可以修改为其他端口,但是修改完之后要修改防火墙

启动服务

java -jarr eleme.....(刚刚上传的eleme.....)

打开8080端口

成功访问,java02和java01操作一样

--部署LVS-dr模式

java02和01一样

然后再访问即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值