Mac (M1)系统下载、安装MySQL
一、下载MySQLMySQL下载地点
https://img-blog.csdnimg.cn/direct/d9d5fd6778f34951a494aa7d33228144.png
二、安装mysql
2.1 MySQL安装
双击安装包
https://img-blog.csdnimg.cn/direct/710d5ac3c47347489a88faa7a66256d8.png
https://img-blog.csdnimg.cn/direct/0e9df15c77294ccc9c26751c3eee8d8b.png
https://img-blog.csdnimg.cn/direct/f6561136fe6c4610ac89be31902ffbb5.png
https://img-blog.csdnimg.cn/direct/8c107e1ada51487b801bf144fd0315be.png
https://img-blog.csdnimg.cn/direct/87818acf0a92424b957c40ebf4d83036.png
https://img-blog.csdnimg.cn/direct/6e5778a801264a2c95ba3d742886aad1.png
到此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
https://img-blog.csdnimg.cn/direct/036e150619dc450d8a5c57f5b77740d5.png
最后按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服务器配置文件(启动、关闭..)存放地点 https://img-blog.csdnimg.cn/direct/3d01282a363a4d95b89fe4ec182a9cfb.png
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
default-character-set=utf8
#password = your_password
port = 3306
socket = /tmp/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
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=<host>, MASTER_PORT=<port>,
# MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
# where you replace <host>, <user>, <password> by quoted strings and
# <port> 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 = <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user = <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password = <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port =<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
quick
max_allowed_packet = 16M
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
default-character-set=utf8
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
interactive-timeout
按 esc键,输入:wq!(注意有冒号:),最后按enter键退出。
2. 修改my.cnf读写权限
在终端输入如下代码:
sudo chmod 664 /etc/my.cnf
3. 在系统设置的MySQL面板中设置配置文件
https://img-blog.csdnimg.cn/direct/049031bd41254c688e365236630f6837.png
当安装MySQL后没有在控制面板指定配置文件,使用Navicat连接服务器时会报2002错误
4. 重新启动MySQL服务
https://img-blog.csdnimg.cn/direct/63ff99dfe496428a90b022e3bf750f01.png
https://img-blog.csdnimg.cn/direct/f2d0fe2e59124eac988e4331e463ac2c.png
三、使用MySQL过程中遇到的BUG
3.1 无法连接MySQL服务器
使用Navicat连接MySQL报错:
https://img-blog.csdnimg.cn/direct/ba0941b8286e467f9b0dccdfa2c327e8.png
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。
[*] 找到并编辑以下参数:
bind-address = 0.0.0.0
或者指定特定的 IP 地点:
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 服务器的连通性,确保能够成功发送和吸收数据包。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]