How to install Mysql on Mac? How to configure Mysql? And how to start and use MySQL

Foreword:

There are many development partners who use mac, so how to install, configure, and use Mysql on mac. Today I will give you a systematic tutorial.

 Install Mysql 

Before using mysql, we need to download mysql first, and follow the following steps to successfully download and install mysql.

 1: Download mysql

First, enter the address https://www.mysql.com/downloads/ in the browser, or log in to the official website of Mysql: https://www.mysql.com/downloads/; as shown in the figure below:

After entering the official website, scroll to the bottom of the page, find Mysql Community (GPL) Downloads, click to download, as shown in the figure below:

Then select: Mysql Community Server.

After clicking Mysql Community Server, enter the following page. There are download links for multiple platforms. We choose the download link of "DMG format"   

In this step of downloading, you need to pay attention to the following two points:

1: Pay attention to the platform (Select Operating System)

    If you are a window, choose window, if you are a mac computer, choose macOs.

2: Pay attention to the chip structure (Select OS Version)

   Chips will be selected according to the platform. For example, macOs is divided into ARM architecture instruction set and X86 architecture.

Because my mac has an m1pro chip, I chose ARM. After selecting the corresponding version, click download directly until the download is completed.

 2:Install mysql

After the download is completed, double-click to open it. The following interface will pop up, and then continue clicking, as shown in the figure below:

The installation process is basically just click Next. Only when the installation reaches the Configuration step shown in the picture above, as shown in the picture below:

At this time, you need to pay attention to the following two points:

  1. Be sure to select Use Legacy Password Encryption. (Note: Because I have chosen Use Strong Password Encryption before, but sometimes there will be problems running the project)
  2. Be sure to remember to enter your password. This password is also the password for logging in to mysql, which is very important. Note: If it is MySQL after version 8.23, you need to enter at least 8 digits when entering the password.​ 

Then just continue to click Next until the installation is complete.

3: Check whether Mysql is installed successfully

Check whether the installation is successful: In the system preferences, check whether there is mysql. If so, the installation is successful.

To check the version of mysql, open the terminal and enter mysql -u root -p

mysql -u root -p

The above picture status indicates that MySQL has been successfully entered. And it shows that the MySQL version is 8.1.0.

 4: Configure Mysql 

If executed in the terminalmysql -u root -p the command appearscommand not found,< /span>It may be that the environment variables are not configured. Open the mac terminal and execute the following command:

vim ~/.bash_profile

After opening the .bash_profile file, press the letter i to enter the editing mode and add the statement PATH=$PATH:/usr/local/mysql/bin,as shown in the figure below

After the environment variables are configured, execute thesource ~/.bash_profile command to make the environment variable configuration take effect.

source ~/.bash_profile

If the configuration is successful, enter the command: mysql -u root -p. The running effect is as follows:

Note: Since the installation path of mysql is different, when configuring the environment variablePATH=$PATH:/usr/local/mysql/bin , the path may be different, we need to configure it according to the path of our own installation. Specific precautions and error prompts will be explained in detail in the detailed explanation of error reporting when using mysql below.

Detailed use of MySQL

Before using mysql, we need to start the related services of mysql.

The default user name of mysql is root. The password assigned by the system is difficult to remember. We need to reset a password:

1: First close the mysql service in the system setting method or through the command in the terminal

Click to close mysql in the settings, as shown in the figure below:

Close the mysql service through the command in the terminal


// 关闭
sudo /usr/local/mysql/support-files/mysql.server stop


2: Use the command in the terminal to start the mysql service in safe mode:

sudo /usr/local/mysql/bin/mysqld_safe –skip-grant-tables,

3: Open another terminal and execute the command: (The password required at this time is the user’s power-on password)

sudo /usr/local/mysql/bin/mysql -u root

4: Execute the change password command in the terminal to reset the password:

UPDATE mysql.user SET authentication_string=PASSWORD(‘12345678’) WHERE User=’root’;,或UPDATE mysql.user SET Password =PASSWORD(‘admin123’) WHERE User=’root’;

5: Refresh FLUSH PRIVILEGES:

FLUSH PRIVILEGES;

6: After the default installation is completed, the mysql service will be started by default, which can be viewed in System Preferences->MySQL:

When you see the prompt shown in the picture above, it means that the MySQL service is turned on! ! !

7: Manually start or pause the mysql service

// 启动:
sudo /usr/local/mysql/support-files/mysql.server start
// 关闭
sudo /usr/local/mysql/support-files/mysql.server stop

Common MySQL errors and how to deal with them

When explaining above to enter mysql and check the mysql version, we executed themysql -u root -p command:

mysql -u root -p

Enter the user password, which is the password you set when you installed mysql above:

The appearance of the above picture status indicates that MySQL has been successfully entered.

tip: If command not found appears here, the solution is as follows:

cd /usr/local/bin/
sudo ln -fs /usr/local/mysql/bin/mysql mysql

Cause analysis:
Because mac can only recognize the mysql command in the /usr/local/bin path by default.
The command path of mysql installed on my mac is in /usr/local/mysql/bin/, so mysql related commands can only be in /usr/local/mysql/bin by default. /path takes effect.


If you directly use the command mysql -u root -p to connect to the mysql database, the error mysql: command not found will be reported.

A summary of commonly used MySQL visualization tools and installation and usage tutorials

There are many commonly used visualization tools for MySql. Here we mainly introduce Navicat Premium. Includes Navicat Premium 16.2 activation and cracking permanent tutorial 2023 latest (including windows+mac), the reference link is as follows:

     1. Navicat Premium 16.2 activation and cracking permanent tutorial 2023 latest (including windows+mac)

Navicat Premium 16.2 activation and cracking permanent tutorial 2023 latest (including windows+mac) - Bilibili

     2. Tutorial on how to open and use MySQL under MacOS

How to open and use MySQL_mac to start mysql__xwh's blog under MacOS - CSDN blog

Some common operations of MySQL

  1. Create a database: create database database name
  2. View databases: show databases;
  3. Delete database: drop database database name
  4. Open the database: use database name
  5. Create table
  6. Add, delete, modify, query SQL statements, etc.
  7. The exit command exits mysql
// 创建表(personId设置了关键值则不能重复)
create table userInfo (personId int,name varchar(20), sex varchar(10), age int, primary key(personId)); 

// 在表中添加新列
alter table userInfo add age int;

// 查看表结构信息
desc personTable;

// 删除表
drop table userInfo;

// 复制表
create table newUserInfo like userInfo;

Let's use the userInfo table created above to perform some simple and commonly used SQL statement operations:

Common SQL operations are mainly divided into four categories: add, delete, modify, and check, that is:

  1. Query data: select * from table name where field = value;
  2. Insert data: insert into table name (field 1, …) values ​​(value 1, …);
  3. Update data: update table name field=value,...,fieldn=valuen where field=value;
  4. Delete data: delete from table name where field=value;
// 查看数据
select * from userInfo; // 查询全部
select * from userInfo where age = 35; // 条件查询
// 插入数据
insert into userInfo (personId, name, sex, age) values (1000, 'admin', "male", 35);
// 更新数据
update userInfo set age = 25,name = "uiChen",sex='female' where personId = 1001;
// 删除数据
delete from userInfo where age = 23;

Guess you like

Origin blog.csdn.net/ljx1400052550/article/details/133048563