Home Database Mysql Tutorial Common MySQL problems and solutions

Common MySQL problems and solutions

Nov 21, 2017 am 09:43 AM
mysql solution question

As programmers, MySQL is definitely something we will use, and it is very important. However, sometimes some problems will inevitably occur in the MySQL database at work, so how do we deal with them? Let's talk about some common MySQL problems and solutions that we encounter in our daily work.

1. Forgot the root password of MySQL

1. Log in to the server where the database is located and manually kill mysql process.

(1) Log in to the server where the database is located, and manually kill the MySQL process:

root@bogon:/data/mysql# kill `cat ./mysql.pid`

Among them, mysql.pid refers to the pid file in the MySQL data directory, which records the process number of the MySQL service.

(2) Use the --skip-grant-tables option to restart the MySQL service:

zj@bogon:/data/mysql$ sudo /usr/local/mysql/bin/mysqld -- The skip-grant-tables --user=root &

--skip-grant-tables option means to skip the permission table authentication when starting the MySQL service. Once started, root will not require a password to connect to MySQL.

(3) Connect to mysql with the root user with an empty password, and change the root password:

zj@bogon:/usr/local/mysql/bin$ mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3Server version: 5.7.18-log Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> set password = password('123456');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statementMySQL [(none)]> use mysql
Database changed
MySQL [mysql]> update user set authentication_string=password('123456') where user="root" and host="localhost";
Query OK, 1 row affected, 1 warning (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 1MySQL [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MySQL [mysql]> exit;
Bye
****************************************************************
zj@bogon:/usr/local/mysql/bin$ mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7Server version: 5.7.18-log Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
Copy after login

Since the --skip-grant-tables option is used to start, use the "set password" command If the password change fails, the password change is successful after directly updating the authentication_string field of the user table (the test version is 5.7.18, the password field of some versions is 'password'). Refresh the permission table to re-validate the permission authentication. When you log in as root again, you can use the password you just changed.

2. How to deal with table damage in the myisam storage engine

Sometimes you may encounter table damage in myisam. Symptoms of a corrupted table are usually that queries are interrupted unexpectedly and the following error is seen:

'table_name.frm' is locked for changes

Cannot find file 'tbl_name.MYYI' (errcode: nnn)

Unexpected end of file

Log file destroyed

Got error nnn from table processor.

There are usually two solutions:

1. Use the myisamchk tool

Use the myisamchk tool that comes with MySQL to repair:

shell> myisamchk -r tablename

The -r parameter means recover. The above method can solve almost all problems. If not, use the command:

shell> mysiamchk -o tablename

The -o parameter means --safe-recover, which allows for safer repair.

2. Use sql command

Use MySQL's check table and repair table commands to repair together. check table is used to check whether the table is damaged; repair table is used to repair the bad table.

3. The problem of insufficient disk space in the data directory

After the system goes online, as the amount of data continues to increase, you will find that the available space in the data directory is getting smaller and larger. Small, thus causing security risks to the application.

1. For the tables of the myisam storage engine

For the tables of the myisam storage engine, you can use the following options when creating the table to separately specify the data directory and index directory to be stored in different disk spaces, and By default, it will be placed in the data directory at the same time:

data directory = 'absolute path to directory'index directory = 'absolute path to directory'

If the table has been created, you can only Stop or lock the table to prevent table changes, then mv the table's data files and index files to a sufficient partition on the disk, and then create a symbolic link in the original file.

2. For tables of innodb storage engine

Because the data files and index files are stored together, they cannot be separated. When disk space is insufficient, a new data file can be added and placed on a disk with sufficient space.
The specific implementation method is to add this file in the parameter innodb_data_file_path, and the path is written as the absolute path of the new disk.
For example, if there is insufficient space under /home and you want to add a new file under /home1 that can automatically expand data, then the parameters can be written like this:

innodb_data_file_path = /home/ibdata1:2000M;/home1 /ibdata2:2000M:autoextend

After the parameters are modified, the database must be restarted to take effect.

4. Problems with DNS reverse resolution (versions after 5.0 skip domain name reverse resolution by default)

Execute the show processlist command on the client, and sometimes many will appear Process, similar to:

unauthenticated user | 192.168.10.10:55644 | null | connect | null | login | null

These processes will accumulate more and more, and will not will disappear and the application cannot respond normally, resulting in system paralysis.

By default, MySQL will perform reverse resolution of the domain name for the IP address of the remote connection. If there is no corresponding domain name in the system's hosts file, MySQL will consider the connection as an invalid user, so An unauthenticated user appears in the next process and causes the process to block.

The solution is very simple. Add the --skip-name-resolve option when starting, then MySQL can skip the domain name resolution process and avoid the above problems.

5. How to connect to the database after mysql.sock is lost

When connecting to the database on the MySQL server itself, it often appears that mysql.sock does not exist, resulting in the inability to connect. The problem. This is because if you specify localhost as a hostname, mysqladmin defaults to using a Unix socket file connection instead of tcp/ip. This socket file (usually named mysql.sock) is often deleted for various reasons. Through the --protocol=TCP|SOCKET|PIPE|MEMORY option, users can explicitly specify the connection protocol. The following demonstrates a successful connection using the tcp protocol after a Unix socket failure.

1. Unix socket connection:

zj@bogon:~$ mysqlERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

2. tcp connection

zj@bogon:~$ mysql --protocol=TCP

this This article shares five problems and solutions that MySQL may encounter. I hope it can help everyone. If you find it useful, please collect it.

Related recommendations:

How to set up the most secure MySQL database?

View the index method of MySQL data table

Questions about MySQL triggers

The above is the detailed content of Common MySQL problems and solutions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

See all articles