步骤分明地教你在MAC上安装MYSQL

本篇文章带大家简析下MAC安装MYSQL的方法,介绍一下MYSQL的一些知识,希望对大家有所帮助!

下载地址:https://downloads.mysql.com/archives/community/

百度网盘下载:链接:https://pan.baidu.com/s/1nceFzJKK7_dJh2gUAtVgWw 密码:qm5z

595c67a38e11e6daa01c01301f8a9e8.png

安装

双击下载好的dmg文件,会弹出pkg弹框,再双击pkg图标,进入安装界面,在安装界面上一路继续,就安装成功了。

环境变量

第一步,在终端切换到根目录,编辑./.bash_profile文件

➜  ~ cd ~
➜  ~ vim ./.bash_profile

第二步,进入vim编辑环境,按下i进入insert模式,输入

export PATH=$PATH:/usr/local/mysql/bin
export PATH=$PATH:/usr/local/mysql/support-files

第三步,按下 esc 退出 insert 模式,输入 :wq 保存配置文件

:wq

第四步,在终端界面输入以下命令,让配置文件的修改生效,并查看环境变量是否设置成功

➜  ~ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
➜  ~ source ~/.bash_profile 
➜  ~ echo $PATH            
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin:/usr/local/mysql/support-files
➜  ~

MYSQL服务的启停和状态的查看

查看MYSQL的服务状态

➜  ~ sudo mysql.server status
Password:
 ERROR! MySQL is not running

启动MYSQL服务

➜  ~ sudo mysql.server start 
Starting MySQL
.Logging to '/usr/local/mysql/data/mj.local.err'.
 SUCCESS!

停止MYSQL服务

➜  ~ sudo mysql.server stop  
Shutting down MySQL
. SUCCESS!

重启MYSQL服务

➜  ~ sudo mysql.server restart
 ERROR! MySQL server PID file could not be found!
Starting MySQL
. SUCCESS!

启动

第一步,终端界面下输入

➜  ~ sudo mysql.server start 
Starting MySQL
.Logging to '/usr/local/mysql/data/mj.local.err'.
 SUCCESS!

第二步,启动MYSQL服务,启动成功后继续输入

➜  ~ mysql -u root -p

第三步,直接回车,进入数据库,看到以下欢迎界面

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

备注:默认安装成功后没有初始密码,所以密码不用输入,直接回车即可。

初始化设置

设置初始密码,进入mysql数据库之后执行下面的语句,设置当前root用户的密码为root

➜  ~ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('root');
Query OK, 0 rows affected (0.01 sec)

mysql>

退出MYSQL的界面

mysql> exit
Bye
➜  ~

配置

进入到 /usr/local/mysql/support-files目录,里面有个文件my-default.cnf

➜  ~ cd /usr/local/mysql/support-files
➜  support-files ll
total 64
-rwxr-xr-x  1 root  wheel   1.1K  6 15  2018 binary-configure
-rw-r--r--  1 root  wheel   773B  6 15  2018 magic
-rw-r--r--  1 root  wheel   1.1K  6 15  2018 my-default.cnf
-rwxr-xr-x  1 root  wheel   894B  6 15  2018 mysql-log-rotate
-rwxr-xr-x  1 root  wheel    10K  6 15  2018 mysql.server
-rwxr-xr-x  1 root  wheel   1.0K  6 15  2018 mysqld_multi.server
➜  support-files

将目录下的my-default.cnf文件复制到桌面上,改名为my.cnf,

➜  support-files ll
total 64
-rwxr-xr-x  1 root  wheel   1.1K  6 15  2018 binary-configure
-rw-r--r--  1 root  wheel   773B  6 15  2018 magic
-rw-r--r--  1 root  wheel   1.1K  6 15  2018 my-default.cnf
-rwxr-xr-x  1 root  wheel   894B  6 15  2018 mysql-log-rotate
-rwxr-xr-x  1 root  wheel    10K  6 15  2018 mysql.server
-rwxr-xr-x  1 root  wheel   1.0K  6 15  2018 mysqld_multi.server
➜  support-files cp my-default.cnf /Users/a1/Desktop/my.cnf

将内容替换为下面的内容

[mysqld]
default-storage-engine=INNODB
character-set-server=utf8
port = 3306

[client]
default-character-set=utf8

将修改后的my.cnf文件复制到/etc目录下,重启MYSQL

➜  /etc cp /Users/a1/Desktop/my.cnf ./
cp: ./my.cnf: Permission denied
➜  /etc sudo cp /Users/a1/Desktop/my.cnf ./
Password:
➜  /etc ll
total 1064
......
-rw-------   1 root  wheel   7.3K  2 29 14:10 master.passwd
-rw-r--r--   1 root  wheel   1.2K  5 17 17:24 my.cnf
-rw-r--r--   1 root  wheel    11B  2 29 14:43 nanorc
-rw-r--r--   1 root  wheel    53B  2 29 14:09 networks
......
➜  /etc

备注:拷贝文件到etc目录需要系统权限,因此需要在命令前加sudo

检测修改结果

66181b6f8a9a0fe0caede6ef5081f0c.png

➜  ~ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like '%char%';
+--------------------------+-----------------------------------------------------------+
| Variable_name            | Value                                                     |
+--------------------------+-----------------------------------------------------------+
| character_set_client     | utf8                                                      |
| character_set_connection | utf8                                                      |
| character_set_database   | latin1                                                    |
| character_set_filesystem | binary                                                    |
| character_set_results    | utf8                                                      |
| character_set_server     | latin1                                                    |
| character_set_system     | utf8                                                      |
| character_sets_dir       | /usr/local/mysql-5.6.41-macos10.13-x86_64/share/charsets/ |
+--------------------------+-----------------------------------------------------------+
8 rows in set (0.01 sec)

mysql>

备注:此时不输入密码就登录不了数据库了,必须使用修改后的密码登录数据库了,并且数据库的字符集编码信息已经修改了。

至此,就可以愉快的使用数据库了!!!

推荐学习:《mysql视频教程》

以上就是步骤分明地教你在MAC上安装MYSQL的详细内容,更多请关注其它相关文章!