Mac (M1)系统下载、安装MySQL
文章目录
- 一、下载MySQL
- 二、安装mysql
- 2.1 MySQL安装
- 2.2 MySQL配置
- 2.2.1 环境变量配置
- 1. 打开终端,输入以下命令:
- 2. 按`i`键,进入`insert`模式,输入以下两行代码
- 3. 回到终端,输入以下命令
- 4. 按`i`键,进入`insert`模式,输入以下两行代码
- 5. 在终端执行如下命令
- 2.2.2 配置文件的创建
- 1. 创建 my.cnf文件
- 2. 修改my.cnf读写权限
- 3. 在系统设置的MySQL面板中设置配置文件
- 4. 重新启动MySQL服务
- 三、使用MySQL过程中遇到的BUG
- 3.1 无法连接MySQL服务器
- 3.1.1 原因分析
- 3.1.2 解决方案
一、下载MySQL
MySQL下载地址
二、安装mysql
2.1 MySQL安装
双击安装包
到此MySQL安装完成。
2.2 MySQL配置
2.2.1 环境变量配置
1. 打开终端,输入以下命令:
vim ~/.bash_profile
2. 按i键,进入insert模式,输入以下两行代码
export PATH=$PATH:/usr/local/mysql/bin # mysql安装目录中的bin目录路径 export PATH=$PATH:/usr/local/mysql/support-files
最后按esc键,输入:wq!(注意有冒号:),然后按回车键退出
3. 回到终端,输入以下命令
vim ~/.zshrc
4. 按i键,进入insert模式,输入以下两行代码
export PATH=$PATH:/usr/local/mysql/bin # mysql安装目录中的bin目录路径 export PATH=$PATH:/usr/local/mysql/support-files #mysql服务器配置文件(启动、关闭..)存放地址
5. 在终端执行如下命令
source ~/.zshrc source ~/.bash_profile
上述命令用于重新加载当前用户的 Zsh shell 配置文件 .zshrc和Bash shell 配置文件 .bash_profile。执行这个命令后,新的配置将立即生效,而无需重新启动终端。
2.2.2 配置文件的创建
①在Windows下的Mysql中,安装目录修改my.ini文件就会对默认字符集进行配置
②而在Mac下,默认没有配置文件,需要自己手动创建my.cnf文件来配置
1. 创建 my.cnf文件
在终端输入
sudo vim /etc/my.cnf
系统提示输入电脑密码,之后进入输入模式,按i键,输入以下内容(不需要做任何修改):
# Example MySQL config file for medium systems. # # This is for a system with little memory (32M - 64M) where MySQL plays # an important part, or systems up to 128M where MySQL is used together with # other programs (such as a web server) # # MySQL programs look for option files in a set of # locations which depend on the deployment platform. # You can copy this option file to one of those # locations. For information about these locations, see: # http://dev.mysql.com/doc/mysql/en/option-files.html # # In this file, you can use all long options that a program supports. # If you want to know which options a program supports, run the program # with the "--help" option. # The following options will be passed to all MySQL clients [client] default-character-set=utf8 #password = your_password port = 3306 socket = /tmp/mysql.sock # Here follows entries for some specific programs # The MySQL server [mysqld] character-set-server=utf8 init_connect='SET NAMES utf8' port = 3306 socket = /tmp/mysql.sock skip-external-locking key_buffer_size = 16M max_allowed_packet = 1M table_open_cache = 64 sort_buffer_size = 512K net_buffer_length = 8K read_buffer_size = 256K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M # Don't listen on a TCP/IP port at all. This can be a security enhancement, # if all processes that need to connect to mysqld run on the same host. # All interaction with mysqld must be made via Unix sockets or named pipes. # Note that using this option without enabling named pipes on Windows # (via the "enable-named-pipe" option) will render mysqld useless! # #skip-networking # Replication Master Server (default) # binary logging is required for replication log-bin=mysql-bin # binary logging format - mixed recommended binlog_format=mixed # required unique id between 1 and 2^32 - 1 # defaults to 1 if master-host is not set # but will not function as a master if omitted server-id = 1 # Replication Slave (comment out master section to use this) # # To configure this host as a replication slave, you can choose between # two methods : # # 1) Use the CHANGE MASTER TO command (fully described in our manual) - # the syntax is: # # CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=, # MASTER_USER=, MASTER_PASSWORD= ; # # where you replace , , by quoted strings and # by the master's port number (3306 by default). # # Example: # # CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306, # MASTER_USER='joe', MASTER_PASSWORD='secret'; # # OR # # 2) Set the variables below. However, in case you choose this method, then # start replication for the first time (even unsuccessfully, for example # if you mistyped the password in master-password and the slave fails to # connect), the slave will create a master.info file, and any later # change in this file to the variables' values below will be ignored and # overridden by the content of the master.info file, unless you shutdown # the slave server, delete master.info and restart the slaver server. # For that reason, you may want to leave the lines below untouched # (commented) and instead use CHANGE MASTER TO (see above) # # required unique id between 2 and 2^32 - 1 # (and different from the master) # defaults to 2 if master-host is set # but will not function as a slave if omitted #server-id = 2 # # The replication master for this slave - required #master-host = # # The username the slave will use for authentication when connecting # to the master - required #master-user = # # The password the slave will authenticate with when connecting to # the master - required #master-password = # # The port the master is listening on. # optional - defaults to 3306 #master-port = # # binary logging - not required for slaves, but recommended #log-bin=mysql-bin # Uncomment the following if you are using InnoDB tables #innodb_data_home_dir = /usr/local/mysql/data #innodb_data_file_path = ibdata1:10M:autoextend #innodb_log_group_home_dir = /usr/local/mysql/data # You can set .._buffer_pool_size up to 50 - 80 % # of RAM but beware of setting memory usage too high #innodb_buffer_pool_size = 16M #innodb_additional_mem_pool_size = 2M # Set .._log_file_size to 25 % of buffer pool size #innodb_log_file_size = 5M #innodb_log_buffer_size = 8M #innodb_flush_log_at_trx_commit = 1 #innodb_lock_wait_timeout = 50 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash # Remove the next comment character if you are not familiar with SQL #safe-updates default-character-set=utf8 [myisamchk] key_buffer_size = 20M sort_buffer_size = 20M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout
按 esc键,输入:wq!(注意有冒号:),最后按enter键退出。
2. 修改my.cnf读写权限
在终端输入如下代码:
sudo chmod 664 /etc/my.cnf
3. 在系统设置的MySQL面板中设置配置文件
当安装MySQL后没有在控制面板指定配置文件,使用Navicat连接服务器时会报2002错误
4. 重新启动MySQL服务
三、使用MySQL过程中遇到的BUG
3.1 无法连接MySQL服务器
使用Navicat连接MySQL报错:
3.1.1 原因分析
①MySQL 服务器未运行;
②错误的连接参数:在 Navicat 连接设置中,您可能未正确配置连接参数。请检查以下项目:
-
主机名/地址:确保正确输入 MySQL 服务器的 IP 地址或主机名。默认情况下,‘127.0.0.1’ 表示本地主机。
-
端口号:确保端口号与 MySQL 服务器配置的端口一致。默认情况下,MySQL 使用 3306 端口。
-
用户名和密码:输入正确的数据库用户名和密码。确保您拥有连接到 MySQL 服务器的权限。
-
防火墙设置:可能是因为防火墙阻止了与 MySQL 服务器的连接。请确保您的防火墙设置允许 Navicat 连接到
-
MySQL 服务器。尝试在防火墙中添加例外规则来允许进出连接。
-
MySQL 配置问题:某些 MySQL 配置可能导致无法连接。请确保 MySQL 服务器配置中的以下设置正确:
- 绑定地址:确保 MySQL 服务器绑定到正确的 IP 地址。如果要允许本地连接,请确保绑定地址包含 ‘127.0.0.1’ 或 ‘localhost’。
- 授权表设置:检查 MySQL 授权表中是否正确设置了允许远程连接的权限。
3.1.2 解决方案
根据问题的可能原因,以下是解决 “Can’t connect to server on ‘127.0.0.1’” 错误的几种常见解决方案:
- 检查 MySQL 服务器状态:确保 MySQL 服务器已启动并正在运行。您可以在命令行或系统服务管理器中检查 MySQL 服务的状态。
如果你想测试 MySQL 服务是否正常运行,可以使用 MySQL 客户端连接到服务器,如下所示:
mysql -h your_mysql_server_ip -u your_username -p
替换 your_mysql_server_ip、your_username 为实际的 MySQL 服务器 IP 地址和用户名。这将尝试连接到 MySQL 服务器,需要输入密码来完成连接。成功连接表示 MySQL 服务正常运行。
-
检查连接参数:仔细检查 Navicat 连接设置中的主机名、端口号、用户名和密码。确保这些参数与 MySQL 服务器的配置相匹配。
-
配置防火墙:确保防火墙允许与 MySQL 服务器的连接。您可以尝试暂时关闭防火墙进行测试,如果连接成功,则说明防火墙设置可能是问题所在。然后,添加允许 Navicat 连接的入站和出站规则。
确保防火墙允许 MySQL 连接的一般步骤,具体步骤可能因操作系统而异:
Linux 上的防火墙配置
- 查看当前防火墙规则:
sudo iptables -L
或者(如果使用 firewalld):
sudo firewall-cmd --list-all
- 允许 MySQL 服务的端口(默认为 3306):
sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
或者(如果使用 firewalld):
sudo firewall-cmd --add-port=3306/tcp --permanent sudo firewall-cmd --reload
macOS 上的防火墙配置
- 查看当前防火墙规则:
sudo pfctl -s rules
- 允许 MySQL 服务的端口(默认为 3306):
sudo pfctl -e sudo echo "pass in proto tcp from any to any port 3306" > /etc/pf.anchors/com.mysql sudo pfctl -f /etc/pf.conf
Windows 上的防火墙配置
- 打开 Windows 防火墙设置:
- 在搜索框中键入 “Windows Defender Firewall with Advanced Security”,然后打开该设置。
- 允许 MySQL 服务的端口(默认为 3306):
- 在 “入站规则” 中,新增规则,选择 “端口”,选择 “下一步”,选择 “特定本地端口” 并输入 3306,选择 “下一步”,选择 “允许连接”,选择 “下一步”,输入规则名称,选择 “完成”。
== 注意事项:==
- 请确保在上述步骤中替换端口号(如果你的 MySQL 服务器不是默认的 3306 端口)。
- 在修改防火墙设置后,建议使用相关的测试方法(如 ping 或 MySQL 客户端连接)来验证连接是否成功。
请记住,允许所有来源的连接可能存在安全风险。最好只允许来自必要主机的连接,并采取其他安全措施,如使用强密码和限制用户权限。
- 检查 MySQL 配置:查看 MySQL 服务器的配置文件(通常是 my.cnf 或 my.ini)以确保绑定地址设置正确,并且授权表中允许了远程连接。
设置绑定地址
在 MySQL 的配置文件中,bind-address 参数用于指定 MySQL 服务器监听的 IP 地址。默认情况下,MySQL 只监听本地回环地址 127.0.0.1,即仅允许本地连接。如果你希望允许远程连接,需要将 bind-address 设置为相应的 IP 地址或者 0.0.0.0,表示接受来自任何 IP 地址的连接。
以下是配置 MySQL 绑定地址的步骤:
-
打开 MySQL 的配置文件,通常是 my.cnf 文件。该文件可能位于 /etc/my.cnf、/etc/mysql/my.cnf 或者 MySQL 安装目录的 my.cnf。
-
找到并编辑以下参数:
[mysqld] bind-address = 0.0.0.0
或者指定特定的 IP 地址:
[mysqld] bind-address = your_specific_ip
如果你使用 0.0.0.0,表示 MySQL 服务器将接受来自任何 IP 地址的连接。
-
保存并关闭配置文件。
-
重新启动 MySQL 服务器,以使更改生效:
sudo service mysql restart
请注意,修改 bind-address 可能会对安全性产生影响。确保在允许远程连接之前采取适当的安全措施,例如使用强密码,限制远程用户的权限,并通过防火墙等方式控制访问。
设置某用户允许某个IP进行远程访问
在 MySQL 中,你可以通过授权表来设置允许远程连接的权限。通常,你需要授权用户允许从特定主机或所有主机进行远程连接。以下是在 MySQL 中设置远程连接权限的一般步骤:
-
使用 MySQL 命令行客户端登录到 MySQL 服务器:
mysql -u your_username -p
替换 your_username 为你的 MySQL 用户名,然后输入密码。
-
为远程连接的用户授予权限:
-
如果你要允许来自任何主机的连接,可以使用 % 作为主机名:
GRANT ALL PRIVILEGES ON *.* TO 'your_remote_user'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION;
替换 your_remote_user 和 your_password 为实际的远程用户和密码。
-
如果你要限制连接到特定 IP 地址范围,将 % 替换为相应的 IP 地址:
GRANT ALL PRIVILEGES ON *.* TO 'your_remote_user'@'your_remote_ip' IDENTIFIED BY 'your_password' WITH GRANT OPTION;
替换 your_remote_user、your_remote_ip 和 your_password 为实际的远程用户、IP 地址和密码。
-
刷新权限:
FLUSH PRIVILEGES;
这将确保在运行授权命令后刷新 MySQL 的权限表。
-
退出 MySQL 客户端:
EXIT;
或者使用快捷键 \q。
-
请注意,上述步骤中的 WITH GRANT OPTION 表示授予用户向其他用户授予权限的能力。如果你不希望用户具有这个能力,可以省略该选项。
最后,确保 MySQL 服务器的防火墙允许远程连接到 MySQL 的端口(默认为 3306)。在授予远程连接权限时,要特别小心安全性问题,并仅允许必要的主机连接。
- 检查网络连接:确保您的网络连接正常工作。尝试通过 ping 命令检查与 MySQL 服务器的连通性,确保能够成功发送和接收数据包。
-