09 MySQL、Nginx

lix_uan
• 阅读 500

MySQL主从复制

准备工作

# 防火墙开放3306端口号
firewall-cmd --zone=public --add-port=3306/tcp --permanent

firewall-cmd --zone=public --list-ports

# 并将两台数据库服务器启动起来
systemctl start mysqld

主库配置

# 192.168.200.200

# /etc/my.cnf
log-bin=mysql-bin   #[必须]启用二进制日志
server-id=200       #[必须]服务器唯一ID(唯一即可)

systemctl restart mysqld

# 创建数据同步的用户并授权
GRANT REPLICATION SLAVE ON *.* to 'xiaoming'@'%' identified by 'Root@123456';

# 登录Mysql数据库,查看master同步状态
show master status;

从库配置

# 192.168.200.201

# /etc/my.cnf
server-id=201     #[必须]服务器唯一ID

systemctl restart mysqld

# 登录Mysql数据库,设置主库地址及同步位置
change master to master_host='192.168.50.132',master_user='xiaoming',master_password='Root@123456',master_log_file='mysql-bin.000002',master_log_pos=154;

start slave;

# 查看从数据库的状态
show slave status\G;

踩坑

# 从机直接从主机克隆过来,UUID一致,导致主从复制失败
find -name auto.cnf

# 把server-UUID改成不一样的, 或直接删除这个文件

MySQL读写分离

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.0.0-RC1</version>
</dependency>
create database rw default charset utf8mb4;

use rw;

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
spring:
  shardingsphere:
    datasource:
      names:
        master,slave
      # 主数据源
      master:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://192.168.200.200:3306/rw?characterEncoding=utf-8
        username: root
        password: root
      # 从数据源
      slave:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://192.168.200.201:3306/rw?characterEncoding=utf-8
        username: root
        password: root
    masterslave:
      # 读写分离配置
      load-balance-algorithm-type: round_robin #轮询
      # 最终的数据源名称
      name: dataSource
      # 主库数据源名称
      master-data-source-name: master
      # 从库数据源名称列表,多个逗号分隔
      slave-data-source-names: slave
    props:
      sql:
        show: true #开启SQL显示,默认false
  main:
    allow-bean-definition-overriding: true

Nginx下载安装

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel wget

wget https://nginx.org/download/nginx-1.16.1.tar.gz

tar -zxvf nginx-1.16.1.tar.gz

# 配置Nginx编译环境, --prefix 指定的目录,就是我们安装Nginx的目录
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx

# 编译&安装
make & make install

Nginx常用命令

cd /sbin

# 查看版本
./nginx -v

# 检查配置文件
./nginx -t

# 启动
./nginx

# 访问80端口
systemctl stop firewalld

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

# 停止
./nginx -s stop

# 修改配置文件后需要重新加载
./nginx -s reload

# /etc/profile配置环境变量

# 踩坑
# Nginx 访问页面403
vi /etc/selinux/config
SELINUX=disabled

vi nginx.conf

user  root;

nginx -s reload

Nginx应用

部署静态资源

# conf/nginx.conf

server {
    listen 80;                #监听端口    
    server_name localhost;    #服务器名称
    location / {            #匹配客户端请求url
        root html;            #指定静态资源根目录
        index index.html;    #指定默认首页
    }
}

反向代理

server {
    listen 82;
    server_name localhost;
    location / {
        proxy_pass http://192.168.200.201:8080;     #反向代理配置,将请求转发到指定服务
    }
}

负载均衡

#upstream指令可以定义一组服务器, weight配置权重
upstream targetserver{    
    server 192.168.200.201:8080 weight=10;
    server 192.168.200.201:8081 weight=5;
}

server {
    listen       8080;
    server_name  localhost;
    location / {
        proxy_pass http://targetserver;
    }
}
点赞
收藏
评论区
推荐文章

暂无数据

lix_uan
lix_uan
Lv1
学无止境,即刻前行
文章
7
粉丝
7
获赞
0