Summary of MySQLDBA common manuals

Source: Internet
Author: User
Summary of common MySQLDBA manuals. For more information about using mysql, see.

MySQL DBA common manual summary, mysql friends can refer.

1. mysql remote connection commands can be used to remotely import and export data
Mysqldump -- default-character-set = gb2312-hsf-0000000.00-uroot-pxxxxxx mydatabase> d: \ data. SQL
Export the specified database to an external SQL file!
Restore command:
Mysql -- default-character-set = gb2312-hsf-0000000.00-uroot-pxxxxxx mydatabase
2. Three MYSQL upgrade Methods
Installation Method: Binary Package compilation and Installation
First: applicable to any storage engine.

1. download and install the new version of MySQL database, change its port to 3307 (to avoid conflict with the old version 3306), and start the service.

2. Create a database with the same name in the new version.

# Mysqldump-p3307-uroot create mysqlsystems_com

3. Back up the database in the old version.

# Mysqldump-p3306-uroot mysqlsystems_com> mysqlsystems_com.bk

Note: You can also add the-opt option, so that you can use the optimization method to export your database to reduce unknown problems.

4. Import the exported database backup to the new MySQL database.

# Mysql-p3307-uroot mysqlsystems_com <mysqlsystems_com.bk

5. overwrite all the mysql databases in the data directory of the old database to the new version.

# Cp-R/opt/mysql-5.1/data/mysql/opt/mysql-5.4/data (the permission Library remains unchanged)

Note: Everyone knows the importance of this default database.

6. Execute the mysql_upgrade command in the new version. In fact, this command contains the following three commands:

# Mysqlcheck-check-upgrade-all-databases-auto-repair
# Mysql_fix_privilege_tables
# Mysqlcheck-all-databases-check-upgrade-fix-db-names-fix-table-names

Note: during each upgrade, we should execute the mysql_upgrade command. It uses the mysqlcheck command to help us check whether the database is compatible with the new version and fix it at the same time, another important role is to use the mysql_fix_privilege_tables command to upgrade the permission table.

7. Disable the old version, change the port used by the new version of the database to 3306, and restart the new version of MySQL database. At this point, the Database Upgrade in a simple environment is over.

Second, it also applies to any storage engine.

1. Install the new MySQL version first.

2. Back up the database in the old version.

# Mkdir/opt/mysqlsystems_bk; mysqldump-p3306-uroot-tab =/opt/mysqlsystems_bk mysqlsystems_com

Note: The-tab option can generate two types of files with the suffix *. SQL and *. txt under the Backup Directory mysqlsystems_bk. The SQL statement stores the SQL statement of the created table and the TXT statement stores the original data.

3. Next, update the data in the new database version.

# Mysqladmin-p3307-uroot create mysqlsystems_com

# Cat/opt/mysqlsystems_bk/*. SQL | mysql-p3307-uroot mysqlsystems_com (Create Tables)

# Mysqlimport mysqlsystems_com/opt/mysqlsystems_bk/*. txt (Load Data)

4. All subsequent steps are the same as those in step 5, step 6, and step 7 of the first method.

The third method is applicable to the MyISAM storage engine, where all files are copied.

1. install.

2. Copy all. frm,. MYD, And. MYI files from the old mysqlsystems_com database to the same directory of the new version.

3. The subsequent steps are the same as those of the first three steps.

The above are three methods to upgrade MySQL. It seems that there are no problems. In fact, there are many problems in the actual production environment, this requires us to fully understand what new features are added to the new version before the upgrade, and further analyze whether these new features will affect our original applications after the upgrade.

3. MYSQL remote connection failure Solution

It may be that this user has insufficient permissions. View the permission list.

4. MYSQL password forgot Solution

In windows:
Open the command line window and stop mysql service: Net stop mysql
Install MySQL to start mysql. Use mysqld-nt.exe to start MySQL in the bindirectory. Run mysqld-nt -- skip-grant-tables in the command line window.
Then, open a command line window and execute mysql. You do not need to enter a password.
> Use mysql
> Update user set password = password ("new_pass") where user = "root ";
> Flush privileges;
> Exit
Use the task manager to locate the mysqld-nt process and end the process!
After restarting the mysql-nt service, you can log on with a new password.

In linux:
If MySQL is running, killall-TERM mysqld is first killed.
Start MySQL: bin/safe_mysqld -- skip-grant-tables &
You can access MySQL without a password.
Then
> Use mysql
> Update user set password = password ("new_pass") where user = "root ";
> Flush privileges;
Kill MySQL again and start MySQL in a normal way.

5. Change the default Character Set of MYSQL

Method 1: use the SET syntax to specify. If "GLOBAL" is not added, it is only valid for this session.
SET [GLOBAL] character_set_client = utf8;
SET [GLOBAL] character_set_connection = utf8;
SET [GLOBAL] character_set_database = utf8;
SET [GLOBAL] character_set_results = utf8;
SET [GLOBAL] character_set_server = utf8;

Method 2: Use the SET syntax, which is only valid for this session.
Set names 'utf8 ';

Method 3) directly modify my. cnf, add a line of content, and then restart MySQL to make it take effect globally.
Default-character-set = utf8

6. MYSQL slow query analysis tool: mysqldumpslow

Mysqldumpslow command
/Path/mysqldumpslow-s c-t 10/database/mysql/slow-log
This will output 10 SQL statements with the maximum number of records, of which:

*-S indicates the sorting method. c, t, l, and r are sorted by the number of records, time, query time, and number of returned records, ac, at, al, and ar indicate reverse descriptions;
*-T indicates the top n, that is, the number of previous data records returned;
*-G, followed by a regular expression matching mode, which is case insensitive;

For example
/Path/mysqldumpslow-s r-t 10/database/mysql/slow-log
You can obtain up to 10 queries from the returned record set.
/Path/mysqldumpslow-s t-t 10-g "left join"/database/mysql/slow-log
Obtain the query statements containing the left join in the first 10 results sorted by time.



View a mysql Option
Show variables like '% VAR_NAME % ';
Select @ VAR_NAME;


When you manage a MySQL database in Linux, there are always some urgent situations. If the database suddenly becomes under great pressure, as a DBA, you may need some common means or commands to analyze the problem and solve it:

When the database suddenly generates pressure, view the SQL statement being queried: (if there is too much content, it indicates that too many SQL statements are executed concurrently, and the database may be congested and slow, under normal circumstances, there should be very few things, that is, connections are all in Sleep state)
/Usr/local/mysql/bin/mysql-uroot-ppassword databaseName-e "show full processlist" | grep-v Sleep

There are too many running SQL statements. If you can't see them, you need to sort them and check the SQL statements with the longest execution duration:
/Usr/local/mysql/bin/mysql-uroot-ppassword databaseName-e "show full processlist" | grep-v Sleep | sort-k6rn> sort. tmp

If you find that IOWait is very high, check the generation of temporary tables, especially disk tmp table:
/Usr/local/mysql/bin/mysql-uroot-ppassword databaseName-e "show global status like '% tmp % '"

In this way, you can check what the database is busy with. Which of the busy SQL statements is stuck in? Is it creating a temporary disk file, Sending Data, and statistics? Solve the problem based on different reasons

---------------------
Frequently used methods for routine management, redo, and Problem Analysis of Mysql Replication:

To redo the Slave or Master changes, you need to synchronize the Slave with the new Master:
Change master to master_host = IP, master_user = 'replication username', master
_ Password = 'replication Passwrod ', master_log_file = 'Log-bin.000001', master_log_pos = 0;

Export data into SQL text with caution. The table will be locked based on your DB size, leading to blocking other accesses:
Nohup/usr/local/mysql/bin/mysqldump-database DATABASEName-uUserName-pPassWord-lock-all-tables-F> DATA20070519. SQL &
-F, the Master Log will be refreshed. In this way, the above Change Master can be used to synchronize the Slave.

Only export the database structure (NO content)
/Usr/local/mysql/bin/mysqldump-d DATABASEName-uUserName-pPassWord> DATA20070519.structure

Only export database data (no statements for creating table structures, etc)
/Usr/local/mysql/bin/mysqldump-t DATABASEName-uUserName-pPassWord> DATA20070519.data

A problem occurs during synchronization (or other problems). view the specific content of Binlog Based on the location (offset) where the synchronization fails.
/Usr/local/mysql/bin/mysqlbinlog binlogFileName-start-position = offset
Haha, when we run the SQL statement executed by the Master, Slave will report a syntax error. It's strange! However, the cause is found: If the bit content is changed to 1 through the stored procedure, this problem will occur, and then the bit will be changed to tinyint (1 ).

Authorize a Server Load balancer instance to have the copy permission:
Grant replication slave on *. * to username @ IP identified by 'Password ';

View the Slave status:
Show slave status \ G
View Master Status:
Show master status;
Reset Slave (use with caution)
Reset slave;
If a problem occurs in Slave, skip this statement first (make sure that the specific content to be skipped does not affect the subsequent synchronization. Check the Binlog file using the confirmation method ):
Set global SQL _slave_skip_counter = 1; (remember to pause Slave: stop slave first, and then restart Slave: start slave ;)

----------------
Purely Linux-related:
Tcpdump-A "dst port 3306" to view the communication details of port 3306

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.