MySQL database administrator common command manual _ MySQL

Source: Internet
Author: User
MySQL database administrator common command manual bitsCN.com

1. mysql remote connection commands can be used to remotely import and export data

  mysqldump --default-character-set=gb2312 -h255.255.000.00 -uroot -pxxxxxx mydatabase>d:/data.sql

Export the specified database to an external SQL file! Restore command:

  mysql --default-character-set=gb2312 -h255.255.000.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 for 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, or step 7 of the first method.

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

1. installation.

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 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 in the bindirectory to start MySQL. 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 the parameter. if "GLOBAL" is not added, the parameter 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 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

For routine management, redo, and problem analysis of Mysql Replication, we often use the following methods: redo Slave, or Master change. we need to synchronize 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 (without any 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

BitsCN.com

The above is the MySQL database administrator's frequently-used Command manual _ MySQL. For more information, see PHP Chinese network (www.php1.cn )!

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.