Mac下mysql安装配置过程

Mysql是一款被广泛使用数据库软件。本文记录了Aaron在Mac下安装和配置Mysql的过程。

安装

官方网站下载合适的mysql community版本。我下载的是mysql-5.6.27-osx10.9-x86_64.dmg

download1

download2

双击安装即可。看网上教程说什么开机启动的pkg,我的dmg中只有一个pkg。

download3

终端下启动mysql报错。-u后面是用户名,-p回车后输入密码。默认用户名密码是root/root

$ mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

配置

查了半天才找到原因。原因是server没有启动。进入mysql安装路径下的support-files启动一下mysql.server

$ cd /usr/local/mysql-5.6.27-osx10.8-x86_64/support-files
$ sudo ./mysql.server start
Password:
Starting MySQL
. SUCCESS!

这样就可以在终端下进入mysql了。

$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.27 MySQL Community Server (GPL)

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>

默认是用户名密码是弱密码,因此需要建立新的用户。

创建用户有两种方法,一种是用insert into,但这种方法我测试时报错。如下:

mysql> insert into mysql.user(Host, User, Password) values("localhost", "testUser", password("somepassword"));
ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value

另一种是用GRANT:

mysql> grant all privileges on *.* to 'testUser'@'localhost' identified by 'somepassword' with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
  • all privileges是授予新用户权限。这里赋予新用户全部权限
  • testUser 是新用户的名字
  • somepassword 相应替换成新用户密码
  • with grant option 允许新用户赋予其他用户权限。
  • flush privileges刷新权限表,使授权生效。

至此,新用户创建完毕。

通过exit退出mysql,然后用新用户登录测试。

mysql> exit
Bye
$ mysql -utestUser -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.27 MySQL Community Server (GPL)

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>
写于2015年10月13日