Complete Guide to Installing, Configuring, and Using MySQL in macOS Terminal

Nov 20, 2025 · Programming · 10 views · 7.8

Keywords: MySQL | macOS | Terminal Installation | Database Configuration | Root Password

Abstract: This article provides a comprehensive guide on installing, configuring, and using MySQL database via the terminal on macOS. Starting from the DMG installer, it covers steps to start the MySQL service, connect to the database through the terminal, handle root password issues, and perform basic database operations. Addressing common challenges like unfamiliar terminal commands and password setup, it offers clear solutions and practical tips to help users quickly master MySQL database management.

Installation and Configuration of MySQL on macOS

For macOS users, the installation process of MySQL is relatively straightforward. It is recommended to use the official DMG installer, which can be downloaded from the MySQL official website. During installation, the system guides users through all necessary configuration steps. After installation, users need to start the MySQL server in System Preferences. This ensures the database service runs properly in the background, preparing for subsequent terminal connections.

Connecting to MySQL Server via Terminal

The core command to connect to the MySQL server is /usr/local/mysql/bin/mysql -u root -p. Here, -u root specifies the username as root, and the -p option prompts for the password. In newer MySQL versions, the installation process automatically generates a temporary password, which must be entered to connect successfully. If connection fails due to path issues, try adding the MySQL bin directory to the system path.

Handling Root User Password Issues

After MySQL installation, the handling of the root user password varies by version. In older versions, the root user might have no password, allowing direct connection with /usr/local/mysql/bin/mysql -u root. However, in recent versions, a random password is generated, and users must use it for the first login. After logging in, it is advisable to change the password immediately using the SQL command ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password'; to enhance security.

Basic Database Operation Examples

Once connected, users can begin performing basic database operations. For instance, use SHOW DATABASES; to view existing databases, CREATE DATABASE database_name; to create a new database, and USE database_name; to select a database. For table operations, CREATE TABLE is used to create tables, INSERT INTO to add data, and SELECT to query data. These commands form the foundation of learning SQL and should be mastered through practice.

Common Issues and Solutions

macOS users often encounter issues where the terminal does not recognize the mysql command, typically because the system path does not include the MySQL bin directory. Solutions include using the absolute path to execute commands or adding the directory to the path via sudo sh -c 'echo /usr/local/mysql/bin > /etc/paths.d/mysql', then restarting the terminal. Additionally, if the password is forgotten, refer to the official documentation for password reset guidelines.

Summary and Best Practices

When using MySQL on macOS, it is recommended to always connect and operate via the terminal to familiarize oneself with the command-line environment. Change the root password immediately after installation and regularly back up important data. Beginners should start with simple database and table operations, gradually progressing to more complex SQL statements. By combining official documentation and community resources, users can efficiently master MySQL usage skills.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.