day109-smbms准备工作

Posted GUGU

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day109-smbms准备工作相关的知识,希望对你有一定的参考价值。

smbms项目部署环境

创建项目

 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion><groupId>com.gu</groupId>
   <artifactId>smbms</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>war</packaging><name>smbms Maven Webapp</name>
   <!-- FIXME change it to the project\'s website -->
   <url>http://www.example.com</url><properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <maven.compiler.source>1.7</maven.compiler.source>
     <maven.compiler.target>1.7</maven.compiler.target>
   </properties><dependencies>
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.11</version>
       <scope>test</scope>
     </dependency><dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
       <version>4.0.1</version>
     </dependency><dependency>
       <groupId>javax.servlet.jsp</groupId>
       <artifactId>jsp-api</artifactId>
       <version>2.2</version>
     </dependency><dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>8.0.28</version>
     </dependency><dependency>
       <groupId>javax.servlet.jsp.jstl</groupId>
       <artifactId>jstl-api</artifactId>
       <version>1.2</version>
     </dependency><dependency>
       <groupId>taglibs</groupId>
       <artifactId>standard</artifactId>
       <version>1.1.2</version>
     </dependency>
   </dependencies></project>

 

配置web.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                       http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
          version="4.0"
          metadata-complete="true">
     
 <!--    字符编码过滤器-->
     <filter>
         <filter-name>CharacterEncodingFilter</filter-name>
         <filter-class>com.gu.filter.CharacterEncodingFilter</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>CharacterEncodingFilter</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
 </web-app>

 

链接数据库

 
driver=com.mysql.jdbc.Driver
 url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
 username=root
 password=123456

 

创建数据库公共类

 package com.gu.dao;
 ​
 import java.io.IOException;
 import java.io.InputStream;
 import java.sql.*;
 import java.util.Properties;
 ​
 //操作数据库的公共类
 public class BaseDao 
 ​
     private static String driver;
     private static String url;
     private static String username;
     private static String password;
 ​
     //静态代码块,类加载的时候就初始化了
     static 
         //通过类加载器读取相应的资源
         InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
         Properties properties = new Properties();
         try 
             properties.load(is);
          catch (IOException e) 
             e.printStackTrace();
         
 ​
         driver = properties.getProperty("driver");
         url = properties.getProperty("url");
         username = properties.getProperty("username");
         password = properties.getProperty("password");
     
 ​
     //获取数据库链接
     public static Connection getConnection()
         Connection connection = null;
         try 
             Class.forName(driver);
             connection = DriverManager.getConnection(url,username,password);
          catch (Exception e) 
             throw new RuntimeException(e);
         
 ​
         return connection;
     
 ​
     //编写查询公共类
     public static ResultSet execute(Connection connection, String sql,Object[] params,ResultSet resultSet,PreparedStatement preparedStatement) throws SQLException 
         preparedStatement = connection.prepareStatement(sql);
 ​
         for (int i = 0; i < params.length; i++) 
             preparedStatement.setObject(i+1,params[i]);
             
         
         resultSet = preparedStatement.executeQuery();
         return resultSet;
 ​
     
 ​
     //编写增删改公共方法
     public static int execute(Connection connection, String sql,Object[] params,PreparedStatement preparedStatement) throws SQLException 
         preparedStatement = connection.prepareStatement(sql);
 ​
         for (int i = 0; i < params.length; i++) 
             preparedStatement.setObject(i+1,params[i]);
 ​
         
         int updateRows = preparedStatement.executeUpdate();
         return updateRows;
 ​
     
 ​
     //释放资源
     public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet)
         boolean flag = true;
         if (resultSet != null)
             try 
                 resultSet.close();
                 resultSet = null;
              catch (SQLException e) 
                 e.printStackTrace();
                 flag = false;
             
         
 ​
         if (preparedStatement != null)
             try 
                 preparedStatement.close();
                 preparedStatement = null;
              catch (SQLException e) 
                 e.printStackTrace();
                 flag = false;
             
         
 ​
         if (connection != null)
             try 
                 connection.close();
                 connection = null;
              catch (SQLException e) 
                 e.printStackTrace();
                 flag = false;
             
         
         return flag;
     
 

 

创建实体类(以一个为例)

 
package com.gu.pojo;
 ​
 import java.util.Date;
 ​
 public class User 
    private Integer id; //id 
    private String userCode; //用户编码
    private String userName; //用户名称
    private String userPassword; //用户密码
    private Integer gender;  //性别
    private Date birthday;  //出生日期
    private String phone;   //电话
    private String address; //地址
    private Integer userRole;    //用户角色
    private Integer createdBy;   //创建者
    private Date creationDate; //创建时间
    private Integer modifyBy;     //更新者
    private Date modifyDate;   //更新时间
    
    private Integer age;//年龄
    
    private String userRoleName;    //用户角色名称
    
    
    public String getUserRoleName() 
       return userRoleName;
    
    public void setUserRoleName(String userRoleName) 
       this.userRoleName = userRoleName;
    
    public Integer getAge() 
       /*long time = System.currentTimeMillis()-birthday.getTime();
       Integer age = Long.valueOf(time/365/24/60/60/1000).IntegerValue();*/
       Date date = new Date();
       Integer age = date.getYear()-birthday.getYear();
       return age;
    
    public Integer getId() 
       return id;
    
    public void setId(Integer id) 
       this.id = id;
    
    public String getUserCode() 
       return userCode;
    
    public void setUserCode(String userCode) 
       this.userCode = userCode;
    
    public String getUserName() 
       return userName;
    
    public void setUserName(String userName) 
       this.userName = userName;
    
    public String getUserPassword() 
       return userPassword;
    
    public void setUserPassword(String userPassword) 
       this.userPassword = userPassword;
    
    public Integer getGender() 
       return gender;
    
    public void setGender(Integer gender) 
       this.gender = gender;
    
    public Date getBirthday() 
       return birthday;
    
    public void setBirthday(Date birthday) 
       this.birthday = birthday;
    
    public String getPhone() 
       return phone;
    
    public void setPhone(String phone) 
       this.phone = phone;
    
    public String getAddress() 
       return address;
    
    public void setAddress(String address) 
       this.address = address;
    
    public Integer getUserRole() 
       return userRole;
    
    public void setUserRole(Integer userRole) 
       this.userRole = userRole;
    
    public Integer getCreatedBy() 
       return createdBy;
    
    public void setCreatedBy(Integer createdBy) 
       this.createdBy = createdBy;
    
    public Date getCreationDate() 
       return creationDate;
    
    public void setCreationDate(Date creationDate) 
       this.creationDate = creationDate;
    
    public Integer getModifyBy() 
       return modifyBy;
    
    public void setModifyBy(Integer modifyBy) 
       this.modifyBy = modifyBy;
    
    public Date getModifyDate() 
       return modifyDate;
    
    public void setModifyDate(Date modifyDate) 
       this.modifyDate = modifyDate;
    
 

 

设置字符监听器

 package com.gu.filter;
 ​
 import javax.servlet.*;
 import java.io.IOException;
 ​
 public class CharacterEncodingFilter implements Filter 
 ​
     @Override
     public void init(FilterConfig filterConfig) throws ServletException 
 ​
     
 ​
     @Override
     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException 
         servletRequest.setCharacterEncoding("UTF-8");
         servletResponse.setCharacterEncoding("UTF-8");
         filterChain.doFilter(servletRequest, servletResponse);
     
 ​
     @Override
     public void destroy() 
 ​
     
 

 

over

 

MySQL主从介绍准备工作准备工作配置从测试主从同步

17.1 MySQL主从介绍

  • MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步;
  • MySQL主从是基于binlog的,主上须开启binlog才能进行主从。
  • 主从过程大致有3个步骤
    • 主将更改操作记录到binlog里
    • 从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里
    • 从根据relaylog里面的sql语句按顺序执行
  • 主上有一个log dump线程,用来和从的I/O线程传递binlong
  • 从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个sql线程用来把relaylog里面的sql语句落地
    MySQL主从原理图:
    技术分享图片

17.2 准备工作

  • 安装mysql

备注:

#设置开机启动
[[email protected] ~]# chkconfig mysqld on

请查阅之前的文章:http://blog.51cto.com/3622288/2056837 12.2小节

17.3 配置主

  • 修改my.cnf
[[email protected] ~]# vi /etc/my.cnf

#增加server-id=130和log_bin=taoyuan
socket=/tmp/mysql.sock #如下增加
server-id=12 #可以自定义,如设定为IP地址192.168.0.12 中的12
log_bin=taoyuan
  • 修改完配置文件后,启动或重启mysqld服务
[[email protected] ~]# /etc/init.d/mysqld restart;

#查看文件
[[email protected] ~]# cd /data/mysql/

[[email protected] mysql]# ls -lt
-rw-rw----  1 mysql mysql       34 1月  23 16:57 taoyuan.index
-rw-rw----  1 mysql mysql      120 1月  23 16:57 taoyuan.000002
-rw-rw----  1 mysql mysql      143 1月  23 16:57 taoyuan.000001
#上述文件,必须有,不然主从无法完成
  • 把mysql可备份并恢复成taoyuan库,作为测试数据
    • mysqldump -uroot mysql > /tmp/mysql.sql
    • mysql -uroot -e "create database taoyuan"
    • mysql -uroot taoyuan < /tmp/mysql.sql
  • 创建用作同步数据的用户
#创建用户
mysql> grant replication slave on *.* to ‘repl‘@‘192.168.0.10‘ identified by ‘taoyuan‘;
Query OK, 0 rows affected (0.00 sec)

#锁表,防止再次写入数据
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.03 sec)

#记住位置
mysql> show master status;
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| taoyuan.000002 |   660574 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

17.4 配置从

  • 修改配置文件
[[email protected] ~]# vi /etc/my.cnf
#增加server-id 跟主不一样 可以设置成10
#log_bin 不需要设置,主才需要生成二进制文件,从不用

#重启服务
[[email protected] ~]# /etc/init.d/mysqld restart
  • 数据同步
#采用复制虚拟机操作,如果没有可以用如下的命令进行同步
scp 192.168.0.12:/tmp/*.sql /tmp/

#恢复库
mysql> create database taoyuan;
Query OK, 1 row affected (0.00 sec)

mysql> create database blog;
Query OK, 1 row affected (0.01 sec)
  • 实现主从
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host=‘192.168.0.12‘, master_user=‘repl‘, master_password=‘taoyuan‘, master_loog_file=‘taoyuan.000002‘, master_log_pos=660574;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
#填写show master status; 显示的信息

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
  • 查看主从是否配置成功
mysql> show slave status\G

#还需到主上执行 unlock tables;

17.5 测试主从同步

  • 主上 mysql -uroot taoyuan
  • select count(*) from db;
  • truncate table db;
  • 到从上mysql -uroot aming;
  • select count(*) from db;
  • 主上继续drop table db;
  • 从上查看db表

  • 几个配置参数
    • 主服务器上
    • binlog-do-db= //仅同步指定的库
    • binlog-ignore-db= //忽略指定库
    • 从服务器上
    • replicate_do_db=
    • replicate_ignore_db=
    • replicate_do_table=
    • replicate_ignore_table=
    • 如下两个常用
    • replicate_wild_do_table= //如taoyuan.%,支持通配符%
    • replicate_wild_ignore_table=

mysql主从配置uuid相同错误解决

配置mysql主从时,由于是拷贝的mysql目录,导致主从mysql uuid相同, Slave_IO无法启动,报错信息如下:

The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
解决办法:修改mysql data 目录下auto.cnf 文件中uuid的值,使两台mysql不同即可,修改后重启mysql服务。

以上是关于day109-smbms准备工作的主要内容,如果未能解决你的问题,请参考以下文章

cgb2108-day16

新工作 Day23 周五

开始一个 Python 项目前要准备什么 - RebuildBlog - Day1

cgb2109-day10

day3

自学it18大数据笔记-第三阶段Spark-day14;Spark-day15(开始试水找工作了)——会持续更新……