标签 mysql 下的文章

mysql与sqlite的数据迁移


一、
1.把sqlite数据库文件和sqlite-to-mysq.py文件放到linux系统下同一文件夹。linux系统得是安装了python2.7版本的
2.找到mysql的my.ini文件,修改为max_allowed_packet=50M,添加wait_timeout=288000 interactive_timeout = 288000 两个配置。max_allowed_packet是mysql允许最大的数据包,也就是你发送的请求; wait_timeout是等待的最长时间,这个值大家可以自定义,但如果时间太短的话,超时后就会现了MySQL server has gone away #2006错误。max_allowed_packet参数的作用是,用来控制其通信缓冲区的最大长度。
如果是linux系统。则找到my.cnf 添加那三条命令
3.重启你的mysql服务
4.执行sqlite3 sqlite文件名 .dump | ./sqlite3-to-mysql.py -u mysql数据库账号 -p mysql数据库密码 -d 迁移到mysql后的数据库名字 | mysql -u root -p --default-character-set=utf8 命令
二、
在数据库管理软件里,选择sqlite中你要迁移的数据库表,右键选择转储SQL文件->数据和结构。然后打开,把里面的"表名"全部替换成表名

sqlite3-to-mysql.py代码:

#! /usr/bin/env python
import re, fileinput, tempfile
from optparse import OptionParser


IGNOREDPREFIXES = [
    'PRAGMA',
    'BEGIN TRANSACTION;',
    'COMMIT;',
    'DELETE FROM sqlite_sequence;',
    'INSERT INTO "sqlite_sequence"',
]

REPLACEMAP = {"INTEGER PRIMARY KEY": "INTEGER AUTO_INCREMENT PRIMARY KEY",
    "AUTOINCREMENT": "AUTO_INCREMENT",
    "DEFAULT 't'": "DEFAULT '1'",
    "DEFAULT 'f'": "DEFAULT '0'",
    ",'t'": ",'1'",
    ",'f'": ",'0'",
}

def _replace_match_allcase(line, src, dst):
    line = line.replace(src,dst)
    line = line.replace(src.lower(),dst)
    return line

def _replace(line):
    if any(line.startswith(prefix) for prefix in IGNOREDPREFIXES):
        return
    for (src,dst) in REPLACEMAP.items():
        line = _replace_match_allcase(line, src, dst)
    return line

def _backticks(line, in_string):
    """Replace double quotes by backticks outside (multiline) strings

    >>> _backticks('''INSERT INTO "table" VALUES ('"string"');''', False)
    ('INSERT INTO `table` VALUES (\\'"string"\\');', False)

    >>> _backticks('''INSERT INTO "table" VALUES ('"Heading''', False)
    ('INSERT INTO `table` VALUES (\\'"Heading', True)

    >>> _backticks('''* "text":http://link.com''', True)
    ('* "text":http://link.com', True)

    >>> _backticks(" ');", True)
    (" ');", False)

    """
    new = ''
    for c in line:
        if not in_string:
            if c == "'":
                in_string = True
            elif c == '"':
                new = new + '`'
                continue
        elif c == "'":
            in_string = False
        new = new + c
    return new, in_string

def _process(opts, lines):
    if opts.database:
        yield '''\
drop database IF EXISTS {d};
create database {d} character set utf8;
grant all on {d}.* to {u}@'localhost' identified by '{p}';
use {d};\n'''.format(d=opts.database, u=opts.username, p=opts.password)
    yield "SET sql_mode='NO_BACKSLASH_ESCAPES';\n"

    in_string = False
    for line in lines:
        if not in_string:
            line = _replace(line)
            if line is None:
                continue
        line, in_string = _backticks(line, in_string)
        yield line

def _removeNewline(line, in_string):
    new = ''
    for c in line:
        if not in_string:
            if c == "'":
                in_string = True
        elif c == "'":
            in_string = False
        elif in_string:
            if c == "\n":
                 new = new + 'Newline333'
                 continue
            if c == "\r":
                 new = new + 'carriagereturn333'
                 continue
        new = new + c
    return new, in_string
    
def _replaceNewline(lines):
    for line in lines:
           line = line.replace("Newline333", "\n")
           line = line.replace("carriagereturn333", "\r")
           yield line

def _Newline(lines):
    in_string = False
    for line in lines:
        if line is None:
           continue
        line, in_string = _removeNewline(line, in_string)
        yield line
    
def main():
    op = OptionParser()
    op.add_option('-d', '--database')
    op.add_option('-u', '--username')
    op.add_option('-p', '--password')
    opts, args = op.parse_args()
    lines = (l for l in fileinput.input(args))
    lines = (l for l in _Newline(lines))
    f = tempfile.TemporaryFile()
    for line in lines:
        f.write(line)
    f.seek(0)
    lines = (l for l in f.readlines())
    f.close()
    lines = (l for l in _process(opts, lines))
    for line in _replaceNewline(lines):
       print line,

if __name__ == "__main__":
    main()

mysql查询某个日期


查询昨天的数据

SELECT * FROM 表名 WHERE DATEDIFF(字段,NOW())=-1;

-- 同理,查询前天的就是-2
查询某一年 某一月 某一天的数据(可组合)

select * fromymt where date_format(inDate,'%Y-%m-%d')='2016-10-10';

(inDate为表中列名)

mysql查询一天,查询一周,查询一个月内的数据
查询一天

select * from ymt where DATE(inDate)=CURDATE(); 
select * from ymt where to_days(inDate)=to_days(now());

查询一周:

select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= DATE(column_time);

查询一个月:

select * fromtable where DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= DATE(column_time);

查询最近7天数据

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)

查询最近30天数据

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

本月

SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )

上一月

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1

查询本季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());

查询上季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

查询本年数据

select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());

查询上年数据

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

查询当前这周的数据

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());

查询上周的数据

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;

查询当前月份的数据

select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')

查询距离当前现在6个月的数据

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

centos7+mysql5.6配置数据库主从同步


1.环境:
操作系统 :CentOS 7
数据库版本:MySQL 5.6.39
主机A:192.168.2.68(Master)
主机B:192.168.2.71 (Slave)
2.先关闭两台主机的防火墙:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

然后可以在两台机子之间进行 ping操作,确保两台机器之间能够相通。
3.Master的配置
打开 /etc/my.cnf ,在该文件下指定Master的配置如下:

log-bin=mysql-bin
server-id=2
binlog-do-db=t1
binlog-ignore-db=mysql

把这几行加在[mysqld]下面
这里的server-id用于标识唯一的数据库,这里设置为2,在设置从库的时候就需要设置为其他值。

binlog-ignore-db:表示同步的时候忽略的数据库 
binlog-do-db:指定需要同步的数据库

4.然后重启mysql:service mysqld restart
进入mysql:mysql -uroot -p
5.赋予从库权限帐号,允许用户在主库上读取日志,赋予192.168.2.71也就是Slave机器有File权限,只赋予Slave机器有File权限还不行,还要给它REPLICATION SLAVE的权限才可以。
在Master数据库命令行中输入:

GRANT FILE ON . TO '用于同步的用户名'@'192.168.1.2' IDENTIFIED BY '用于同步的用户密码';
GRANT REPLICATION SLAVE ON . TO '用于同步的用户名'@'192.168.1.2' IDENTIFIED BY '用于同步的用户密码';
FLUSH PRIVILEGES;

上面命令一条条执行
6.再次重启mysql服务 再次进入mysql
显示主库信息

show master status;

这里的 File 、Position 是在配置Salve的时候要使用到的,Binlog_Do_DB表示要同步的数据库,Binlog_Ignore_DB 表示忽略的数据库,这些都是在配置的时候进行指定的。
Slave的配置
1、从库的配置,首先也是修改配置文件:/etc/my.cnf 如下:

log-bin=mysql-bin
server-id=3
binlog-ignore-db=mysql
replicate-do-db=t1
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60

2.然后重启mysql:service mysqld restart
进入mysql:mysql -uroot -p
mysql> stop slave; #关闭Slave
mysql> change master to master_host='192.168.2.68',master_user='刚刚你创建的用于同步的用户名',master_password='刚刚你创建的用于同步的用户密码',master_log_file='mysql-bin.刚刚查看主库信息的那个file', master_log_pos=刚刚查看主库信息的那个pos;
mysql> start slave; #开启Slave
以上命令一条条执行
3.查看从库信息

show slave status G

如果 Slave_IO_Running: Yes
Slave_SQL_Running: Yes
这两个都为yes 就是成功了

主库my.cnf配置
1.png

从库my.cnf配置
2.png


centos7安装mysql


yum install wget  
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
yum install mysql
yum install mysql-devel
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum -y localinstall mysql57-community-release-el7-11.noarch.rpm
yum install mysql-community-server
service mysqld restart
grep password /var/log/mysqld.log
set password for 'root'@'%' =password('password');