分类目录归档:数据库

使用xtrabackup 配置主从服务器

1、备份主机数据
innobackupex –user=root –password=xifenfei –defaults-file=/etc/my.cnf –stream /tmp/mysql 2>/tmp/mysql.log | gzip> /tmp/data/mysql.tar.gz

2、查看/tmp/mysql.log,记录下当前日志位置
innobackupex: MySQL binlog position: filename ‘mysql-bin.000018′, position 107

3、备份文件恢复
scp /tmp/data/mysql.tar.gz root@192.168.1.2:/tmp/
mkdir /tmp/mysql
cd /tmp/mysql
tar izxvf /tmp/mysql.tar.gz
–恢复数据库
innobackupex –apply-log –user=root –password=xifenfei /tmp/mysql
–复制到my.cnf中指定地方
innobackupex –copy-back –user=root –password=xifenfei /tmp/mysql
chmod -R mysql.mysql /var/lib/mysql

4、修改my.cnf文件
主服务器:
server-id=1
innodb_flush_log_at_trx_commit=1
sync-binlog=1

从服务器:
server-id=2
relay-log=/var/lib/mysql/replicate
relay-log-index=/var/lib/mysql/replicate.index
read-only

7、添加复制用户(主数据库上)
GRANT REPLICATION SLAVE ON *.*
TO ‘repl’@’192.168.1.2′ IDENTIFIED BY ‘xifenfei’;

8、配置从数据库
CHANGE MASTER TO
MASTER_HOST=’192.168.1.4′,
MASTER_USER=’repl’,
MASTER_PASSWORD=’xifenfei’,
MASTER_LOG_FILE=’mysql-bin.000018′,
MASTER_LOG_POS=107;
start slave;

9、查看主从是否正常
SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.4
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000022
Read_Master_Log_Pos: 1185
Relay_Log_File: replicate.000007
Relay_Log_Pos: 588
Relay_Master_Log_File: mysql-bin.000022
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: 1185
Relay_Log_Space: 1627
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: 1

SHOW MASTER STATUS\G
*************************** 1. row ***************************
File: mysql-bin.000022
Position: 1185
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)

SHOW PROCESSLIST\G
–从服务器进程
*************************** 2. row ***************************
Id: 22
User: system user
Host:
db: NULL
Command: Connect
Time: 1136
State: Waiting for master to send event
Info: NULL
*************************** 3. row ***************************
Id: 23
User: system user
Host:
db: NULL
Command: Connect
Time: 1676370
State: Slave has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
–主服务器进程
*************************** 1. row ***************************
Id: 14
User: repl
Host: 192.168.1.2:34594
db: NULL
Command: Binlog Dump
Time: 1207
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL

10、管理从服务器
start slave;
reset slave;
stop slave;

发表在 MySQL安装配置 | 2 条评论

oracle分割函数

1、创建varry

CREATE OR REPLACE TYPE Varchar2Varray IS VARRAY(100) of VARCHAR2(40);

2、创建分割函数

CREATE OR REPLACE FUNCTION f_strsplit (STRING VARCHAR2, substring VARCHAR2)
   RETURN varchar2varray
IS
   len       INTEGER        := LENGTH (substring);
   lastpos   INTEGER        := 1 - len;
   pos       INTEGER;
   num       INTEGER;
   i         INTEGER        := 1;
   ret       varchar2varray := varchar2varray (NULL);
   v_str     VARCHAR2 (20);
 /**自定义split函数,将指定的字符串按指定的标志符分割成字符数组*/
BEGIN
   LOOP
      pos := INSTR (STRING, substring, lastpos + len);

      IF pos > 0
      THEN                                                            --found
         num := pos - (lastpos + len);
      ELSE                                                         --not found
         num := LENGTH (STRING) + 1 - (lastpos + len);
      END IF;

      IF i > ret.LAST
      THEN
         ret.EXTEND;
      END IF;

      v_str := SUBSTR (STRING, lastpos + len, num);
      --DBMS_OUTPUT.put_line (v_str);

      ret(i) := v_str;

      EXIT WHEN pos = 0;
      lastpos := pos;
      i := i + 1;
   END LOOP;

   RETURN ret;
END;

3、调用

select * from tab_dailyreport_user where user_id in (
  SELECT * FROM 
( TABLE( select f_strsplit(user_invite,',')
                        from tab_dailyreport_user t
                       where t.user_id = 168148)));

说明:主要是改写开发提过来的一个sql语句的帮助

select * from tab_dailyreport_user where user_id in (
--'166445','164216','171128','184427','160113','160133','160134','160138','160144','160163','160587','166457','167914','168076','168192','168997','169842','169901','184407','184747','185089','185130','208134','208141','208143','208183','160142','166455','167838','168074','168194','168666','185088','185138','185145','208103','169844','171071','160107','166421','166874','168193','179871','179872','184096','184228','184232','184269','184774','184969','185111','207871','160167','161813','14880','134','164355','168149')
select substr(regexp_replace,3,length(regexp_replace)-4) from (
select regexp_replace((select user_invite
                        from tab_dailyreport_user t
                       where t.user_id = 168148),
                      '(,)',
                      chr(39)||','||chr(39)) regexp_replace
  from dual) )

括号里面的语句查询出来的结果是注释部分,直接使用注释的部分在in中可以查询,如果使用里面的语句不能查询,他们的需求是想把外面的user_id在里面的语句中的,可是oracle会把里面的in查询出来的结果作为一个整体,从而出现number类型不能和varchar类型匹配的提示,采用方法是分割in里面查询出来的语句,然后类型转换为table进行查询。

发表在 Oracle 开发 | 一条评论

该关注的sql语句

一般来说,调优的第一手资料,很可能就是典型业务期的一个statspack报告,那么如何根据statspack报告来判断是哪些SQL消耗了最多的系统资源?哪些SQL是最需要调整的呢?这里给出了一个大致的优化思路。当然,思路是死的,人是活的,优化也需要随需应变。
  一般来说,需要关注下面四种Top SQL
  消耗最多CPU的(逻辑IO过多)
  导致过多物理I/O的
  执行次数较频繁的
  执行时间较长的
  我们知道,一个语句的响应时间有个很著名的公式:
  响应时间=服务时间+等待时间
  其中服务时间就是CPU为执行该语句花费的时间。
  服务时间=分析时间+递归时间+执行时间
  分析时间是CPU用于分析语句的时间,递归时间是CPU用于语句的递归SQL的时间,剩下的则就是CPU用于执行语句的真正时间了。
  那么,上面的这些时间信息从哪里来的?Oracle提供的系统统计信息中就有部分的时间统计信息:
  服务时间=CPU used by this session
  分析时间=parse time cpu
  递归时间=recursive cpu usage

  那么,执行时间就可以根据上面三个统计信息计算得出:
  执行时间=CPU used by this session – parse time cpu – recursive cpu usage
  如果执行时间在整个响应时间中占较大的比例,那么下一步就是找出那些造成了最多逻辑IO的SQL语句,可以从statspack报告的SQL ordered by Gets部分找到。
  如果分析时间在整个响应时间中占较大的比例,那么下一步就是查找哪些SQL分析过多,这在statspack报告中在SQL ordered by Parse Calls中列出。
  如果等待时间在整个响应时间中占较大的比例,并且主要是块读取相关的等待时,下一步就是找出哪些SQL造成了过多的物理读,可以查看statspack报告中的SQL ordered by Reads部分。
  那么,根据上面列出的一个简单的原则,我们需要关注三个关于CPU时间的统计信息: CPU used by this session, parse time cpu和recursive cpu usage,以及top5等待事件中和IO相关的等待时间。如果是其他的一些等待事件出现在Top5中,那么可能需要根据不同的等待事件来分析原因了。然后优先调优时间消耗最多的相关SQL。
  除了上面的SQL ordered by Gets(逻辑IO最多),SQL ordered by Parse Calls(软解析过多),SQL ordered by Reads(物理IO过多),statspack还按照其他的一些方式列出了Top SQL,这些Top SQL在某些情况下都是需要给予特别关注的。比如:
  SQL ordered by Executions 执行次数超过100的
  SQL ordered by Sharable Memory 占用library cache超过1M的
  SQL ordered by Version Count 子cursor超过20的
  如果没有statspack,那么根据v$sysstat/v$sesstat中的统计信息,结合v$sql/v$sqlarea,一样可以得到相关的SQL。
  v$sql对于每一个子cursor都有一行统计记录,而v$sqlarea则对同一个父cursor只有一行统计记录,也就是v$sqlarea是对v$sql按照父cursor进行group by后的一个结果。这两个视图中都有诸如buffer_gets,parse_calls,disk_reads,,executions,sharable_mem等列,和上面提到的statspack中列出Top SQL的条件对应。

发表在 Oracle | 评论关闭