MySQL FAQ
Installation Issues
Q: What should I do if MySQL service fails to start?
A: Check the following:
- Confirm MySQL is properly installed
- Check error logs:
mysqld --console - Verify port is not in use:
netstat -an | grep 3306 - Ensure data directory permissions are correct
- Try reinitializing the data directory
Q: How do I reset root password if I forgot it?
A:
- Stop MySQL service
- Start with skip-grant-tables:
mysqld --skip-grant-tables - Login to MySQL
- Reset password:
UPDATE mysql.user SET Password=PASSWORD('new_password') WHERE User='root'; - Flush privileges:
FLUSH PRIVILEGES; - Restart MySQL service
Q: How do I change MySQL port number?
A: Modify my.cnf or my.ini configuration file:
[mysqld]
port=3307
Then restart MySQL service.
Connection Issues
Q: What if remote connection is rejected?
A:
- Confirm MySQL allows remote connections (check user's host in user table)
- Check if firewall allows the port
- Ensure bind-address is not 127.0.0.1 in MySQL configuration
- Check SELinux or AppArmor settings
Q: How to handle connection timeouts?
A:
- Check network connection
- Increase wait_timeout configuration
- Use persistent connections or connection pools
- Check server load
Database Operation Issues
Q: How to handle character set garbled text?
A:
- Specify charset when creating database:
CREATE DATABASE db CHARACTER SET utf8mb4; - Set charset when connecting:
SET NAMES utf8mb4; - Set default charset in configuration:
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
Q: How to handle import/export garbled text?
A:
- Specify charset when exporting:
mysqldump --default-character-set=utf8mb4 - Specify charset when importing:
mysql --default-character-set=utf8mb4
Q: Is it normal for auto-increment primary keys to not be sequential?
A: Yes, this is normal. Reasons include:
- Failed inserts consume IDs
- Transaction rollbacks don't reclaim used IDs
- Batch inserts may cause jumps
- Deleting records doesn't affect the auto-increment counter
Performance Issues
Q: What if queries are slow?
A:
- Use EXPLAIN to analyze query execution plan
- Create indexes for columns used in WHERE, JOIN, ORDER BY
- Avoid full table scans
- Optimize queries, avoid SELECT *
- Consider query caching (removed in MySQL 8.0)
- Split tables or databases to reduce pressure
Q: How to view slow queries?
A:
- Enable slow query log:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 2;
- View slow query log location:
SHOW VARIABLES LIKE 'slow_query_log_file';
Q: How to optimize high memory usage?
A:
- Adjust innodb_buffer_pool_size (recommended 70% of available memory)
- Optimize queries to reduce unnecessary scans
- Limit number of connections
- Clean unnecessary caches
Backup and Recovery Issues
Q: What if backup file is too large?
A:
- Use compressed backup:
mysqldump | gzip > backup.sql.gz - Backup databases/tables separately
- Use xtrabackup for physical backups
- Regularly clean historical data
Q: How to implement incremental backup?
A:
- Enable binary logging:
log-bin=mysql-bin - Regularly backup binary logs
- Use mysqlbinlog tool to restore incremental data
Replication Issues
Q: What if master-slave replication lag is too high?
A:
- Check network latency
- Adjust slave_parallel_workers to increase parallel replication
- Ensure slave hardware performance is sufficient
- Optimize master write speed
- Check replication filter rules
Q: How to handle master-slave data inconsistency?
A:
- Resync slave
- Use pt-table-checksum to check inconsistency
- Use pt-table-sync to fix data
- Reconfigure master-slave replication
Security Issues
Q: How to strengthen MySQL security?
A:
- Set strong password policies
- Restrict root user remote login
- Delete anonymous accounts
- Delete test database
- Regularly update MySQL version
- Enable SSL/TLS encrypted connections
- Configure firewall rules
- Follow principle of least privilege
Q: How to prevent SQL injection?
A:
- Use Prepared Statements
- Validate and filter user input
- Limit database user permissions
- Avoid dynamic SQL concatenation
- Use stored procedures
