MySQL FAQ

Installation Issues

Q: What should I do if MySQL service fails to start?

A: Check the following:

  1. Confirm MySQL is properly installed
  2. Check error logs: mysqld --console
  3. Verify port is not in use: netstat -an | grep 3306
  4. Ensure data directory permissions are correct
  5. Try reinitializing the data directory

Q: How do I reset root password if I forgot it?

A:

  1. Stop MySQL service
  2. Start with skip-grant-tables: mysqld --skip-grant-tables
  3. Login to MySQL
  4. Reset password: UPDATE mysql.user SET Password=PASSWORD('new_password') WHERE User='root';
  5. Flush privileges: FLUSH PRIVILEGES;
  6. 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:

  1. Confirm MySQL allows remote connections (check user's host in user table)
  2. Check if firewall allows the port
  3. Ensure bind-address is not 127.0.0.1 in MySQL configuration
  4. Check SELinux or AppArmor settings

Q: How to handle connection timeouts?

A:

  1. Check network connection
  2. Increase wait_timeout configuration
  3. Use persistent connections or connection pools
  4. Check server load

Database Operation Issues

Q: How to handle character set garbled text?

A:

  1. Specify charset when creating database: CREATE DATABASE db CHARACTER SET utf8mb4;
  2. Set charset when connecting: SET NAMES utf8mb4;
  3. Set default charset in configuration:
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

Q: How to handle import/export garbled text?

A:

  1. Specify charset when exporting: mysqldump --default-character-set=utf8mb4
  2. 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:

  1. Use EXPLAIN to analyze query execution plan
  2. Create indexes for columns used in WHERE, JOIN, ORDER BY
  3. Avoid full table scans
  4. Optimize queries, avoid SELECT *
  5. Consider query caching (removed in MySQL 8.0)
  6. Split tables or databases to reduce pressure

Q: How to view slow queries?

A:

  1. Enable slow query log:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 2;
  1. View slow query log location:
SHOW VARIABLES LIKE 'slow_query_log_file';

Q: How to optimize high memory usage?

A:

  1. Adjust innodb_buffer_pool_size (recommended 70% of available memory)
  2. Optimize queries to reduce unnecessary scans
  3. Limit number of connections
  4. Clean unnecessary caches

Backup and Recovery Issues

Q: What if backup file is too large?

A:

  1. Use compressed backup: mysqldump | gzip > backup.sql.gz
  2. Backup databases/tables separately
  3. Use xtrabackup for physical backups
  4. Regularly clean historical data

Q: How to implement incremental backup?

A:

  1. Enable binary logging: log-bin=mysql-bin
  2. Regularly backup binary logs
  3. Use mysqlbinlog tool to restore incremental data

Replication Issues

Q: What if master-slave replication lag is too high?

A:

  1. Check network latency
  2. Adjust slave_parallel_workers to increase parallel replication
  3. Ensure slave hardware performance is sufficient
  4. Optimize master write speed
  5. Check replication filter rules

Q: How to handle master-slave data inconsistency?

A:

  1. Resync slave
  2. Use pt-table-checksum to check inconsistency
  3. Use pt-table-sync to fix data
  4. Reconfigure master-slave replication

Security Issues

Q: How to strengthen MySQL security?

A:

  1. Set strong password policies
  2. Restrict root user remote login
  3. Delete anonymous accounts
  4. Delete test database
  5. Regularly update MySQL version
  6. Enable SSL/TLS encrypted connections
  7. Configure firewall rules
  8. Follow principle of least privilege

Q: How to prevent SQL injection?

A:

  1. Use Prepared Statements
  2. Validate and filter user input
  3. Limit database user permissions
  4. Avoid dynamic SQL concatenation
  5. Use stored procedures