lzqabc

导航

gpmall

1.Mycat数据库分离

  • 配置mycat三节点名字和文件
 hostnamectl set-hostname mycat  
 hostnamectl set-hostname db1  
 hostnamectl set-hostname db2
cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.131 mycat
192.168.200.132 db1
192.168.200.135 db2
 
  • 3节点配置yum源,安装java
# mv /etc/yum.repos.d/* /media/
# cat /etc/yum.repos.d/local.repo 
[mariadb]
name=mariadb
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1
yum clean all
yum repolist

yum  install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
java -version
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)
 
  • db1+db2节点安装mariadb,初始化数据库
# yum install -y mariadb mariadb-server
# systemctl start mariadb
# systemctl enable mariadb
#mysql_secure_installation 
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):    #默认按回车
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:                               #输入数据库root密码123456
Re-enter new password:                        #重复输入密码123456
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n
 ... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
 
  • 配置两节点my.cnf
cat /etc/my.cnf
[mysqld]
log_bin=mysql-bin
binlog_ignore_db=mysql
server_id=132            #和节点ip一致,db2为135
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
 
  • 重启mariadb,并root登录数据库,db1配置权限,db2连接
 systemctl restart mariadb
 mysql -uroot -p123456
 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 137
Server version: 10.3.18-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant all privileges  on *.* to root@'%' identified by "123456";
#在主节点db1数据库上创建一个user用户让从节点db2连接,并赋予从节点同步主节点数据库的权限,命令如下。
MariaDB [(none)]> grant replication slave on *.* to 'user'@'db2' identified by '123456';
 
 mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 88
Server version: 10.3.18-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>  change master to master_host='db1',master_user='user',master_password='123456';
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: db1
                  Master_User: user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000009
          Read_Master_Log_Pos: 527
               Relay_Log_File: mariadb-relay-bin.000019
                Relay_Log_Pos: 811
        Relay_Master_Log_File: mysql-bin.000009
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 527
              Relay_Log_Space: 1391
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 132
1 row in set (0.00 sec)
 

#验证主从数据库的同步功能

create database test;
     use test
     Database changed
create table company(id int not null primary key,name varchar(50),addr varchar(255));
insert into company values(1,"facebook","usa");
select * from company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
elect * from test.company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
 
  • 部署Mycat读写分离中间件服务
tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
chown -R 777 /usr/local/mycat/
#在/etc/profile系统变量文件中添加Mycat服务的系统变量,并生效变量。
echo export MYCAT_HOME=/usr/local/mycat/ >> /etc/profile
source /etc/profile
 
  • 配置文件<只做测试,后面gpmall还要改这个文件>
cat /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<---gpmall要改的文件内容在这
<schema name="USERDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"></schema> 
<dataNode name="dn1" dataHost="localhost1" database="test" />  
--->
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" dbType="mysql" dbDriver="native" writeType="0" switchType="1"  slaveThreshold="100">
    <heartbeat>select user()</heartbeat>
    <writeHost host="hostM1" url="192.168.200.132:3306" user="root" password="123456">
        <readHost host="hostS1" url="192.168.200.135:3306" user="root" password="123456"/>
    </writeHost>
</dataHost>
</mycat:schema>
 
  • 编辑mycat的访问用户
  • 修改root用户的访问密码与数据库,密码设置为123456,访问Mycat的逻辑库为USERDB

cat /usr/local/mycat/conf/server.xml 
在配置文件的最后部分,
<user name="root">
		<property name="password">123456</property>
		<---gpmall文件修改位置
		<property name="schemas">USERDB</property>
		--->
然后删除如下几行:
<user name="user">
		<property name="password">user</property>
		<property name="schemas">TESTDB</property>
		<property name="readOnly">true</property>
</user>
 
  • 启动mycat
/bin/bash /usr/local/mycat/bin/mycat start
netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      16952/java         
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1439/mysqld        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      982/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1448/master        
tcp6       0      0 :::1984                 :::*                    LISTEN      16952/java         
tcp6       0      0 :::8066                 :::*                    LISTEN      16952/java         
tcp6       0      0 :::9066                 :::*                    LISTEN      16952/java         
tcp6       0      0 :::40842                :::*                    LISTEN      16952/java         
tcp6       0      0 :::45072                :::*                    LISTEN      16952/java         
tcp6       0      0 :::22                   :::*                    LISTEN      982/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1448/master        

2: Zookeeper

  • 配置hosts文件
hostnamectl set-hostname zk1  //zk2 //zk3  #3台机器,全是zk1也行
cat /etc/hosts  #3台机器

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.136 zk1   #根据上面的机器名字配置为好
192.168.200.137 zk2
192.168.200.138 zk3
 
  • 配置源
 mv /etc/yum.repos.d/* /media/
 cat /etc/yum.repos.d/local.repo 
[gpmall]
name=gpmall
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1

yum clean all
yum repolist
 
  • 3节点安装java
# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
 
  • 3节点解压zookeeper并配置文件
tar -zxvf zookeeper-3.4.14.tar.gz -C /root
cd /root/zookeeper-3.4.14/conf
mv zoo_sample.cfg zoo.cfg
grep -n '^'[a-Z] zoo.cfg
2:tickTime=2000
5:initLimit=10
8:syncLimit=5
12:dataDir=/tmp/zookeeper
14:clientPort=2181
29:server.1=192.168.200.136:2888:3888
30:server.2=192.168.200.137:2888:3888
31:server.3=192.168.200.138:2888:3888

 cat /tmp/zookeeper/myid #1节点
 1 
  cat /tmp/zookeeper/myid #2节点
 2
  cat /tmp/zookeeper/myid #3节点
 3
 
  • 4启动Zookeeper
cd /root/zookeeper-3.4.14/bin
./zkServer.sh start
  ZooKeeper JMX enabled by default
  Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
./zkServer.sh status
  ZooKeeper JMX enabled by default
  Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
  Mode: follower
        leader   #两follower和一个leader
        follower
 

3: Kafka

  • 3节点解压kafka并配置文件
 tar -zxvf kafka_2.11-1.1.1.tgz -C /root	
 cat /root/kafka_2.11-1.1.1/config/server.properties
#需要注释的
#broker.id=0
#zookeeper.connect=localhost:2181
#listeners=PLAINTEXT://:9092
#文末添加
broker.id=1   #节点2=2   #节点3=3
zookeeper.connect=192.168.200.136:2181,192.168.200.137:2181,192.168.200.138:2181   #3个节点IP
listeners = PLAINTEXT://192.168.200.136:9092 #根据节点IP
 
  • 开启kafka
cd /root/kafka_2.11-1.1.1/bin/
./kafka-server-start.sh -daemon ../config/server.properties
jps   #三个节点
  11416 Kafka
  11464 Jps
  10479 QuorumPeerMain
 #如果包挂不起来,建议reboot三个节点,重新启动zookeeper和kafka
 

4.Redis

  • 修改数据库配置到gpmall
 mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 10.3.18-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database gpmall;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> use gpmall
Database changed
MariaDB [gpmall]> source /root/gpmall.sql
Query OK, 0 rows affected (0.000 sec)
Query OK, 0 rows affected (0.000 sec)
...
 
  • 修改mycat配置文件<schema name修改为gpmall,true修改为false,database修改为gpmall>
cat /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="gpmall" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"></schema> 
<dataNode name="dn1" dataHost="localhost1" database="gpmall" />  
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" dbType="mysql" dbDriver="native" writeType="0" switchType="1"  slaveThreshold="100">  
    <heartbeat>select user()</heartbeat>
    <writeHost host="hostM1" url="172.16.51.22:3306" user="root" password="123456">
        <readHost host="hostS1" url="172.16.51.26:3306" user="root" password="123456" />
    </writeHost>
</dataHost>
</mycat:schema>


###<property name="schemas">USERDB</property>改成<property name="schemas">gpmall</property>
cat /usr/local/mycat/conf/server.xml
<user name="root">
		<property name="password">123456</property>
		<property name="schemas">gpmall</property>
		
		<!-- 表级 DML 权限设置 -->
		<!-- 		
		<privileges check="false">
			<schema name="TESTDB" dml="0110" >
				<table name="tb01" dml="0000"></table>
				<table name="tb02" dml="1111"></table>
			</schema>
		</privileges>
        		 -->
</user>
 
  • 重启mycat并查看8066和9066端口
 netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program        name
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      16952/java         
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1439/mysqld        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      982/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1448/master        
tcp6       0      0 :::1984                 :::*                    LISTEN      16952/java         
tcp6       0      0 :::8066                 :::*                    LISTEN      16952/java         
tcp6       0      0 :::9066                 :::*                    LISTEN      16952/java         
tcp6       0      0 :::40842                :::*                    LISTEN      16952/java         
tcp6       0      0 :::45072                :::*                    LISTEN      16952/java         
tcp6       0      0 :::22                   :::*                    LISTEN      982/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1448/master  
 
  • 安装redis
yum install redis -y
 
  • 配置redis.conf文件
cat /etc/redis.conf
#bind 127.0.0.1 ::1
#bind 192.168.1.100 10.0.0.1
#bind 127.0.0.1
protected-mode no
 
  • 开启redis并自启动,查看6379端口
systemctl start redis
systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.

 netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      16735/redis-server 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1034/sshd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1180/master        
tcp6       0      0 :::6379                 :::*                    LISTEN      16735/redis-server 
tcp6       0      0 :::22                   :::*                    LISTEN      1034/sshd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1180/master        
 

5:JAR1+2

  • 配置yum源,安装java环境
# mv /etc/yum.repos.d/* /media/
#cat /etc/yum.repos.d/local.repo 
[gpmall]
name=gpmall
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1
yum clean all
yum repolist
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
java -version
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)
 
  • 编辑hosts文件<jar1+jar2>
  • #怕出错,把redis和nginx也加一遍
 cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.139  redis.mall
192.168.200.131  mysql.mall
192.168.200.136  kafka1.mall
192.168.200.137  kafka1.mall
192.168.200.138  kafka1.mall
192.168.200.136  zk1.mall
192.168.200.137  zk1.mall
192.168.200.138  zk1.mall
 
  • 运行jar包
nohup java -jar shopping-provider-0.0.1-SNAPSHOT.jar &
nohup java -jar user-provider-0.0.1-SNAPSHOT.jar &
nohup java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar &
nohup java -jar gpmall-user-0.0.1-SNAPSHOT.jar &
ps -aux |grep java
root      86473  0.3  2.3 2688764 11116 pts/0   Sl   09:44   0:14 java -jar shopping-provider-0.0.1
root      88991  1.5 14.3 2724168 69320 pts/0   Sl   10:29   0:15 java -jar user-provider-0.0.1-SNA
root      89217  1.5  7.2 2692792 35104 pts/0   Sl   10:32   0:12 java -jar gpmall-user-0.0.1-SNAPS
root      89539  3.2 33.4 2698948 161400 pts/0  Sl   10:37   0:15 java -jar gpmall-shopping-0.0.1-S
root      90007  0.0  0.2 112720   984 pts/0    R+   10:45   0:00 grep --color=auto java

#使用 tail -f nohup.out或者cat nohup.out查看jar包运行情况,关闭所有节点防火墙
 

Nginx

  • 安装nginx,将dist文件夹内容原封不动的放入html文件夹
 yum install nginx -y
rm -rf /usr/share/nginx/html/*
cp -rvf dist/* /usr/share/nginx/html/
#查看html文件夹下是否为static加index.html,不行请拖入
 
  • 配置nginx文件
cat /etc/nginx/conf.d/default.conf
upstream myuser {
            server 192.168.200.142:8082;
            server 192.168.200.143:8082;
            ip_hash;
        }

upstream myshopping {
           server 192.168.200.142:8081;
           server 192.168.200.143:8081;
           ip_hash;
        }
upstream mycashier {
           server 192.168.200.142:8083;
           server 192.168.200.143:8083;
           ip_hash;
        }
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;


    location / {
         root   /usr/share/nginx/html;
         index  index.html index.htm;
    }
    location /user {
            proxy_pass http://myuser;
        }

    location /shopping {
            proxy_pass http://myshopping;
        }

    location /cashier {
            proxy_pass http://mycashier;
        }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
 
  • 启动nginx,查看80端口
systemctl start nginx
netstat -tunpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      18121/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1018/sshd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1152/master        
tcp6       0      0 :::80                   :::*                    LISTEN      18121/nginx: master
tcp6       0      0 :::22                   :::*                    LISTEN      1018/sshd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1152/master        
udp        0      0 127.0.0.1:323           0.0.0.0:*                           648/chronyd        
udp        0      0 0.0.0.0:68              0.0.0.0:*                           4777/dhclient      
udp6       0      0 ::1:323                 :::*                                648/chronyd        

posted on 2022-09-19 14:01  李子权  阅读(48)  评论(0编辑  收藏  举报