Skip to content

Instantly share code, notes, and snippets.

@binderclip
Last active September 19, 2020 08:52
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save binderclip/2e69ae8578a1f762d510 to your computer and use it in GitHub Desktop.
Save binderclip/2e69ae8578a1f762d510 to your computer and use it in GitHub Desktop.
MySQL Installation on Mac

MySQL 安装和上手使用 on Mac

安装

之前看 MySQL 的入门教程的时候发现不是很容易找到和自己环境完全一样的方法,后来问同事,作比较终于走通了。安装方法不只是一种,用的相关 APP 也不是一种,每种都是可行的,但是这里只描述自己更偏好的那一种 :D。这篇文章假设读者开始对 MySQL 都没有一个概念,所以除了软件的安装和配置,还会简单介绍一点点关于 MySQL 的东西。

首先 $ brew install mysql 安装 mysql,之后可能需要重启一下终端,然后成功的话输入 $ mysql 再按 Tab 会提示一些和 mysql 相关的命令出来,如果有的话就 OK 了,如下:

$ mysql
mysql                       mysql_zap
mysql.server                mysqladmin
mysql_client_test           mysqlbinlog
mysql_client_test_embedded  mysqlbug
mysql_config                mysqlcheck
mysql_config_editor         mysqld
mysql_convert_table_format  mysqld_multi
mysql_embedded              mysqld_safe
mysql_find_rows             mysqldump
mysql_fix_extensions        mysqldumpslow
mysql_install_db            mysqlhotcopy
mysql_plugin                mysqlimport
mysql_secure_installation   mysqlshow
mysql_setpermission         mysqlslap
mysql_tzinfo_to_sql         mysqltest
mysql_upgrade               mysqltest_embedded
mysql_waitpid

到这里安装就完成啦!然后是简单的配置和上手使用。

配置和使用

MySQL 和直接使用的 SQLite 不同,需要有一个 MySQL server 在,之后通过 socket 去连接上 MySQL,然后再进行对数据库的相关操作。

启动服务和配置 root 用户

靠下面的命令来启动服务器。

$ mysql.server start
Starting MySQL
.. SUCCESS!

关闭服务器的命令也很简单:

$ mysql.server stop
Shutting down MySQL
.. SUCCESS!

之后重新开启来进行后面的操作。

MySQL 也有自己的用户系统,所以后面提到的用户不要和操作系统的弄混了。默认的用户是 root,没有密码。可以通过下面命令来设置一个密码:

$ mysqladmin -u root password
New password:
Confirm new password:

包括 mysqladmin 在内,其他的 mysql 需要指明用户的时候加 -u username,如果该用户设置过密码需要添加 -p 指令,所以如果已经设置过 root 用户的密码了之后再想修改密码,用前面的会报错,如下:

$ mysqladmin -u root password
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'

这时候只需要加上 -p 选项就好。

$ mysqladmin -u root password -p
Enter password:
New password:
Confirm new password:

使用 root 用户连接数据库

上面 root 用户已经 OK 了,赶快连接下数据库吧。

$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.6.26 Homebrew

Copyright (c) 2000, 2015, 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> system pwd
/usr/local/Cellar/mysql/5.6.26
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
mysql> CREATE DATABASE mydb;
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
mysql> use mydb;
Database changed
mysql> SHOW TABLES;
Empty set (0.00 sec)

创建一个新用户

root 用户的权限一般不会随便拿去使用,所以创建一些用户,并分配对应的权限。下面的命令创建的是一个名字是 user12 密码是 34klq* 的本地用户:

mysql> CREATE USER user12@localhost IDENTIFIED BY '34klq*';

之后给他分配 mydb 数据库的权限:

mysql> GRANT ALL ON mydb.* to user12@localhost;

简单上手使用

  • mysql.server start
  • 设置 root 密码
  • 用 root 登陆,创建表
  • 创建新用户分配权限
  • 开始简单使用

GUI 工具

为了方便操作和查看,可以使用一下 GUI 工具,Mac 下有一个好用且免费的 Sequel Pro

用 Socket 连接数据库。之后可以进行很多很多操作,慢慢去探索吧。

其他参考资料

上面内容的目的是一个无痛的上手操作,更多的使用可以参考下面的。

SQL 资源索引

@binderclip
Copy link
Author

被自己之前写的给救了,好感恩

@binderclip
Copy link
Author

最后的链接好像打错了

@abxiachao
Copy link

。。。

@lochin712
Copy link

good

@willwei
Copy link

willwei commented May 4, 2017

good

Copy link

ghost commented May 15, 2017

谢谢!

@GSDzuoshao
Copy link

没毛病

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment