mysql six: data backup, pymysql module

1. Introduction to IDE tools

The production environment is still recommended to use the mysql command line, but in order to facilitate our testing, you can use the IDE tool download link: https://pan.baidu.com/s/1bpo5mqj

master:

1. Test + link database

2. Create a new library

3. Create a new table, add fields + types + constraints

4. Design Tables: Foreign Keys

5. New query

6. Backup library/table

Notice:

    Add comments in batches: ctrl+? key

    To comment in batches: ctrl+shift+? key


Two MySQL data backup

1. Explanation of terms

    Physical Backup: Directly copy database files, suitable for large database environments. But it cannot be restored to heterogeneous systems such as Windows.

    Logical backup: The backup is the SQL statement executed by the operations such as table creation, database creation, and insertion. It is suitable for small and medium-sized databases, and the efficiency is relatively low.

    Export Table: Import the table into a text file. 

2. Use mysqldump to implement logical backup

Syntax: mysqldump -h server -u username -p password database name > backup file.sql

Example:

  Single database backup

    mysqldump -uroot -p123 db1 > db1.sql

    mysqldump -uroot -p123 db1 table1 table2 > db1-table1-table2.sql

  Multi-database backup

    mysqldump -uroot -p123 --databases db1 db2 mysql db3 > db1_db2_mysql_db3.sql

  Backup all libraries

    mysqldump -uroot -p123 --all-databases > all.sql 

3. Restore logical backup

Method 1: mysql -uroot -p123 < /backup/all.sql

Method Two:

    mysql> use db1;

    mysql> SET SQL_LOG_BIN=0;

    mysql> source /root/db1.sql

Note: If you back up/restore a single library, you can modify the sql file

    DROP database if exists school;

    create database school;

    use school; 

4. Backup/Restore Case

Experiment 1: Database corruption

  Backup:

    #mysqldump -uroot -p123 --all-databases > /backup/`date +%F`_all.sql

    #mysql -uroot -p123 -e 'flush logs' //truncate and generate new binlog

    insert data //simulate the server is up and running

    mysql> set sql_log_bin=0; //Simulate server damage

    mysql> drop database db;

  recover:

    #mysqlbinlog last binlog > /backup/last_bin.log

    mysql> set sql_log_bin=0; 

    mysql> source /backup/2014-02-13_all.sql //Restore the last full backup 

    mysql> source /backup/last_bin.log //Restore the last binlog file

Experiment 2: If you delete it by mistake

  Backup:

    mysqldump -uroot -p123 --all-databases > /backup/`date +%F`_all.sql

    mysql -uroot -p123 -e 'flush logs' // truncate and generate new binlog

    insert data //simulate the server is up and running

    drop table db1.t1 //Simulate accidental deletion

    insert data //simulate the server is up and running

  recover:

    #mysqlbinlog last binlog --stop-position=260 > /tmp/1.sql 

    #mysqlbinlog last binlog --start-position=900 > /tmp/2.sql 

    mysql> set sql_log_bin=0; 

    mysql> source /backup/2014-02-13_all.sql //Restore the last full backup

    mysql> source /tmp/1.log //Restore the last binlog file

    mysql> source /tmp/2.log //Restore the last binlog file

Precautions:

    1. Fully restore to a clean environment (eg new database or delete old database)

    2. All SQL statements should not be logged to binlog during recovery

5. Realize automatic backup

Backup plan:

    1) What time is 2:00

    2) Which databases are backed up

    3) The location of the backup file

Backup script:

vim /mysql_back.sql

    #!/bin/bash

    back_dir=/backup

    back_file=`date +%F`_all.sql

    user=root

    pass=123

    if [ ! -d /backup ];then

        mkdir -p /backup

    be

    #backup and truncate log

    mysqldump -u${user} -p${pass} --events --all-databases > ${back_dir}/${back_file}

    mysql -u${user} -p${pass} -e 'flush logs'

    #Only keep backups from the last week

    cd $ back_dir

    find . -mtime +7 -exec rm -rf {} \;

Manual test:

    #chmod a+x /mysql_back.sql 

    #chattr +i /mysql_back.sql

    #/mysql_back.sql

Configure cron:

    # crontab -e

    2 * * * /mysql_back.sql

6. Export and import of tables

1) SELECT... INTO OUTFILE to export a text file

    mysql> SELECT * FROM school.student1 INTO OUTFILE 'student1.txt'

        FIELDS TERMINATED BY ',' //Define field separator

        OPTIONALLY ENCLOSED BY '"' // define what symbols are used to enclose the string

        LINES TERMINATED BY '\n' ; //define line break

2) The mysql command exports a text file:

    mysql -u root -p123 -e 'select * from student1.school' > /tmp/student1.txt

    mysql -u root -p123 --xml -e 'select * from student1.school' > /tmp/student1.xml

    mysql -u root -p123 --html -e 'select * from student1.school' > /tmp/student1.html

3) LOAD DATA INFILE import text file

    mysql> DELETE FROM student1;

    mysql> LOAD DATA INFILE '/tmp/student1.txt' INTO TABLE school.student1

        FIELDS TERMINATED BY ','

        OPTIONALLY ENCLOSED BY '”'

        LINES TERMINATED BY '\n';

Note: ERROR 1238 (HY000): Variable 'secure_file_priv' is a read only variable may be reported

The most important thing in the database is the data. Once the database permissions are leaked, the data can be easily exported to a file and then downloaded and taken away through the above statement. Therefore, mysql restricts this and can only export the file to the specified directory.

    [mysqld]

    secure_file_priv='xxx' 

Restart mysql and re-execute the above statement

7. Database Migration

Make sure to migrate between the same versions

    #mysqldump -h source IP -uroot -p123 --databases db1 | mysql -h destination IP -uroot -p456


3. pymysql module

1. Installation

pip3 install pymysql

2. Link, execute sql, close (cursor)

    import pymysql

    user=input('Username: ').strip()

    pwd=input('password: ').strip()

    #Link

    conn=pymysql.connect(host='localhost',user='root',password='123',database='databasename',charset='utf8')

    #cursor

    cursor=conn.cursor() #The result set returned after execution is displayed as a tuple by default

    #cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)

    #Execute sql statement

    sql='select * from tablename where name="%s" and password="%s"' %(user,pwd) #Note that %s needs to be quoted

    print(sql)

    res=cursor.execute(sql) #Execute the sql statement and return the number of records for which the sql query was successful

    print(res)

    cursor.close()

    conn.close()

    if res:

        print('Login successful')

    else:

        print('Login failed')

3. sql injection of execute()


4. Add, delete, modify: conn.commit()


5、查:fetchone,fetchmany,fetchall


6. Get the auto-incrementing ID of the last piece of data inserted


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325234032&siteId=291194637