MySQL 水平拆分(读书笔记整理)(3)

通过数据切分技术将一个大的MySQL Server 切分成多个小的MySQL Server,既解决了写入性能瓶颈问题,同时也再一次提升了整个数据库集群的扩展性。不论是通过垂直切分, 还是水��切分,都能够让系统遇到瓶颈的可能性更小。尤其是当我们使用垂直和水平相结合的切分方法之后,理论上将不会再遇到扩展瓶颈了。

6,案例演示 6.1 创建数据库3个实例

创建多实例参加:

6.2 创建库和表以及用户

创建库表

create database `hwdb` /*!40100 default characterset utf8 */; create table uc_user(user_id bigint primarykey, uc_name varchar(200), created_time datetime) engine=innodb charset utf8;

创建用户

grant insert,update,delete,select on hwdb.*to tim@'192.168.%' identified by 'timgood2013';

执行过程:

mysql> create table uc_user(user_idbigint primary key, uc_name varchar(200), created_time datetime) engine=innodbcharset utf8; Query OK, 0 rows affected (0.53 sec) mysql> mysql> grant insert,update,delete,selecton hwdb.* to tim@'192.168.%' identified by 'timgood2013'; Query OK, 0 rows affected (0.03 sec) mysql>   6.3 创建Java代码示例 package mysql; import java.math.BigInteger; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Calendar; public classMySQLTest { public static void main(String[] args) { MySQLTestmt=newMySQLTest(); // BigIntegerbi = newBigInteger("2015053010401005"); Stringport=mt.getDBPort(bi.longValue()); Connection conn=mt.getConn(port); mt.insert(conn,bi, "tim--"+bi.longValue()); } // 获取要访问的db端口 public String getDBPort(long user_id){ Stringport="3307"; long v_cast=user_id%3; if (v_cast==1 ){ port="3308"; }else if(v_cast==2){ port="3309"; }else { port="3307"; } return port; } // 获取数据库的连接,如果扩展的话,可以单独做一个接口提供给程序员来调用它 public ConnectiongetConn(String port ) { Connectionconn = null; try { Class.forName("com.mysql.jdbc.Driver"); }catch(ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Stringurl = "jdbc:mysql://192.168.52.130:"+port+"/hwdb"; try { conn= DriverManager.getConnection(url, "tim", "timgood2013"); }catch(SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("the current db is :"+url); return conn; } // 获取日期字符串 public StringgetTimeByCalendar(){ Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR);//获取年份 int month=cal.get(Calendar.MONTH);//获取月份 int day=cal.get(Calendar.DATE);//获取日 int hour=cal.get(Calendar.HOUR);//小时 int minute=cal.get(Calendar.MINUTE);//分 int second=cal.get(Calendar.SECOND);//秒 String strdate=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second; return strdate; } // 开始录入数据 public int insert(Connectioncnn,BigInteger user_id,String name){ Stringsql="insert intohwdb.uc_user(user_id,uc_name,created_time)values(?,?,?)"; int i=0; long uid =user_id.longValue(); Connectionconn=cnn; try{ PreparedStatement preStmt=conn.prepareStatement(sql); preStmt.setLong(1, uid); preStmt.setString(2,name); preStmt.setString(3,getTimeByCalendar()); i=preStmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return i;//返回影响的行数,1为执行成功 } } 6.4 测试代码

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/53fdc150d2f27ed22b154f40f808f5e7.html