Python学习笔记——MySQL Server的在Mac OS上的安装和配置

随着使用Python进行金融数据处理的经历越来越丰富、时间越来越长,我也越来越感觉到数据存储对金融数据分析和量化投资的重要性,使用的工具也逐步升级。从最开始的csv文件,到后来的feather,现在越来越觉得这些文件系统渐渐不敷使用。因此现在是时候开始用真正的数据库工具来管理和存储本地数据了。

经过简单的评比分析,选择了MySQL作为主要的数据库工具,前两天安装并配置好了MySQL Server,由于走了一些弯路,因此干脆将整个过程记录下来,一来分享、二来备忘。

MySQL的下载安装

MySQL的官网可以直接找到MySQL的社区开源版本下载,下载网址: https://dev.mysql.com/downloads/mysql/
网站提供了dmg和tar等多种不同的安装方式,而且还有针对M1芯片的版本可选:
image
dmg安装是我比较熟悉的方式,下载后可以直接安装:
image
安装完成后,可以在系统设置中找到MySQL选项:
image
在这里可以查看MySQL状态,启动或关闭MySQL Server。只有启动后才可以连接到服务器使用数据库:
image
接下来进入Terminal,如果此时直接尝试使用mysql,会提示mysql: command not found,需要先将它添加到环境变量:

open -e ~/.bash_profile

在打开的配置文件中添加以下目录(MySQL的安装目录):

export PATH=${PATH}:/usr/local/mysql/bin

关闭文件后,接下来再使其生效:

source ~/.bash_profile

进入MySQL环境

接下来就可以在Terminal中使用以下命令进入MySQL环境并使用SQL命令了。
由于在安装MySQL的时候就会配置root用户的密码,因此可以直接通过下面命令使用root用户登录,密码就是在安装时设置的初始密码:

mysql -u root -p

输入密码后,即可进入MySQL环境,尝试使用SHOW DATABASES;命令显示所有的数据库:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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 DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.11 sec)

mysql> 

创建新的账户

由于root账户只能用于本地登录,为了实现远程登录,还需要创建新的账户,同时还能对账户进行权限管理,在许多老版本的使用示例中,可以使用GRANT语句来创建用户并同时赋予权限,但是现在的8.0版本不再支持这样做,必须先用CREATE创建用户,然后再用GRANT赋予用户权限:

# 创建新的用户,并允许客户通过localhost连接
mysql> CREATE USER '用户名'@'localhost' IDENTIFIED BY '初始密码';
Query OK, 0 rows affected (0.46 sec)

# 设置用户的权限
mysql> GRANT ALL ON *.* TO '用户名'@'localhost';
Query OK, 0 rows affected (0.06 sec)

# 创建新的用户,并允许客户通过远程连接
mysql> CREATE USER '用户名'@'%' IDENTIFIED BY '初始密码';
Query OK, 0 rows affected (0.46 sec)

# 设置用户的权限
mysql> GRANT ALL ON *.* TO '用户名'@'%';
Query OK, 0 rows affected (0.06 sec)

注意使用'用户名'@'%'创建的用户是不能通过localhost访问数据库的,所以需要创建两个客户,分别通过localhost和远程方式登录。下面可以通过两种方式登录一下:

通过localhost方式访问数据库:

mysql -h localhost -u jackie -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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 DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

通过IP地址访问数据库:

MYSQL -h 127.0.0.1 -u jackie -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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 DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
posted @ 2021-08-04 21:35  JackiePENG  阅读(4)  评论(0编辑  收藏  举报  来源