Three ways to copy mysql table + grant permissions to three instructions

How to quickly copy a table

First, create a table db1.t, 1000 and insert rows, creating the same structure as a table db2.t

Suppose, now need to db1.t rows inside a> 900 out of the guide, is inserted into the db2.t

mysqldump method

Several key parameters Notes:

  • -Single-transaction role that does not require additional table lock table db1.t when exporting data, but the use of
    START TRANSACTION WITH CONSISTENT SNAPSHOT method;
  • -no-create-info mean not need to export table structure;
  • -result-file specifies the path to the output file, where the file is generated to represent client on the client machine.

Export csv file

select * from db1.t where a>900 into outfile '/server_tmp/t.csv';

This statement will save the results on the server side. If you execute the command of the client and MySQL server not on the same machine, a temporary directory on the client machine is not going to generate t.csv file.

This command will not help you to overwrite the file, so you need to make sure /server_tmp/t.csv this file does not exist, or when the statement is executed will be because of the presence of the same name files and error.

After obtaining export the .csv file, you can then import the data into the target table db2.t in the load data with the following command.

load data infile '/server_tmp/t.csv' into table db2.t;
  1. Open file /server_tmp/t.csv, by tabs (\ t) as a delimiter between the fields, a newline (\ n) as the separator between the recording, to read the data;
  2. Start a transaction.
  3. Determining the number of fields in each row is the same table db2.t:
    • If not the same, the direct error, the transaction is rolled back;
    • If yes, configured in a row, InnoDB engine interface calls, written to the table.
  4. Repeat step 3 until /server_tmp/t.csv read the entire file is completed, the transaction is committed.

Method physical copy

and a method of deriving mysqldump CSV file, data are turned logical approach, the data is read out from the table db1.t, the generation of text, and then written in the destination table db2.t. Physical data guide the way? For example, copy the .frm file directly to the table and .ibd db1.t file to the db2 directory, feasible? The answer is not acceptable.

Because, a InnoDB table, in addition to containing these two physical files, also need to be registered in the data dictionary. Both direct copy documents, since the data is not in the dictionary db2.t the table, and the system will not accept the identification thereof.

In MySQL 5.6 version introduced with the method of transmitting the table space (transportable tablespace) can be derived by way of introduction + tablespace, functional physical copy of the table.

Suppose now aims at db1 library, an identical copy of the table with the table t r, specific implementation steps:

  1. Performing create table r like t, create an empty table of the same table structure,
  2. Execute alter table r discard tablespace, this time r.ibd file will be deleted
  3. Execution flush table t for export at this time will generate a t.cfg
  4. Performed in the db1 directory cp t.cfg r.cfg; cp t.ibd r.ibd; these two commands;
  5. Execution unlock tables, this time t.cfg file will be deleted;
  6. Performing alter table r import tablespace, this r.ibd file as a new tablespace table r, since the data content and t.ibd this document is the same, so the table and the table r, t have the same data.

Advantages and disadvantages of the three methods

  1. Physical copy fastest way, especially for large copy sheet is the fastest way. But it must be a full copy, the copy can not be a part of, you need to copy the data to the server can not be used when a user can not log database host, and the source and target tables must be innodb engine.

  2. The method comprises generating the file with the mysqldump INSERT statement, the filter may be increased in the condition where the parameters to achieve export only part of the data. One deficiency of this approach is that you can not use join such a complex condition where the wording.

  3. With select ... into outfile method is the most flexible, supports all SQL writing. However, one disadvantage of this method is that you can only export the data in a table, and the table structure also requires an additional statement backed up separately.

    The latter two are logical backup, across engine.

mysql global permissions

  • SELECT * FROM MYSQL.USER WHERE USER='UA'\G 显示所有权限
  • Scope entire mysql, mysql information stored in the user table

  • It gives a user the highest authority ua:

    • grant all privileges on *.* to 'ua'@'%' with grant option;
  • The grant commands for the two actions: respectively mysql.user disk table will have permission to modify the field Y, and the user memory acl_user Objects corresponding access modify the value 'all 1'

  • If a new client with a user name ua successful login, mysql maintains a thread object for the new connection, all judgments about global permissions, are directly saved internal thread object permission bits.

    • For global grant command permissions, and update the corresponding disk and memory, then the newly created connection will use the new authority
    • For connection already exists, and it's not affected by the global grant of permission.
  • If you want to recover the above permissions:

    • revoke all privileges on *.* from 'ua'@'%';
      Two operations is also corresponding to the value of the modified bit 0 access permission fields disks modified bit N, objects in memory.

mysqlDB rights

grant all privileges on db1.* to 'ua'@'%' with grant option;

Use SELECT * FROM MYSQL.DB WHERE USER = 'UA' \ G db permissions to view the current user, the same also modify permissions on disk and in-memory objects.

db privileges are stored in the table mysql.db

Note: and global permissions different impact db connection object permissions might already exist.

mysql table and column privileges Permissions

Table permissions on mysql.tables_priv, the column permissions stored in mysql.columns_priv, the combination of these two types of permissions in the hash stored in the memory structure column_priv_hash.

Db with authority similar to the two grant permission each time will modify the data sheet, the synchronization will be modified hash structures in memory, therefore, the operation of these two types of authority will affect the existing connection.

flush privileges usage scenarios

Some documents mentioned, execute the command flush privileges immediately after the grant, to make empowering statement to take effect. In fact, more accurate to say it is time for privileges in the data table is inconsistent with the rights of data in memory, flush privileges statement can be used to reconstruct the data memory, to achieve a consistent state.

  • For example, a time to delete the recorded data table, but the memory of the data there, leading to user empowerment fail, because there is no record in the data table.
  • At the same time re-create the user does not work, because in the judgment of memory, users will think that there is.

Guess you like

Origin www.cnblogs.com/jimmyhe/p/11220664.html