mysql 5.7.27 解压版 配置过程-macOS 10.14
下载
https://downloads.mysql.com/archives/community/
解压
双击解压,删除mysql的data目录
移动目录和改名
将mysql-5.7.27-macos10.14-x86_64拷贝到/usr/local/目录下
sudo mv mysql-5.7.27-macos10.14-x86_64 /usr/local/mysql
配置环境变量
vim /etc/profile
修改/etc/profile文件,将mysql的路径添加进去:
export MYSQL_HOME=/usr/local/mysql
export PATH=$MYSQL_HOME/bin:$MYSQL_HOME/support-files:$PATH
使执行文件立即生效
source /etc/profile
修改MySQL的用户和组
sudo chown -R root:wheel mysql
初始化
cd /usr/local/mysql
sudo bin/mysqld --initialize --user=mysql
记住密码
2021-08-16T06:21:47.732818Z 1 [Note] A temporary password is generated for root@localhost: 9RCahZ%)kR5g
启动服务
cd /usr/local/mysql
sudo support-files/mysql.server start
查看服务
sudo support-files/mysql.server status
修改root密码
cd /usr/local/mysql/bin
./mysqladmin -u root -p password
输入初始化的密码
9RCahZ%)kR5g
回车,输入两次新密码
root
回车
root
回车
登录进入
./mysql -u root -p
设置数据库编码
查看目前的编码
show variables like 'character%';
创建配置文件my.cnf
cd /usr/local/mysql/support-files/
vi my.cnf
- 内容为:
[mysqld]
#设置3306端口
port = 3306
#设置mysql客户端默认字符集
character-set-server=utf8
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
复制my.cnf到/private/etc
sudo cp /usr/local/mysql/support-files/my.cnf /private/etc/my.cnf
重启服务
cd /usr/local/mysql
sudo support-files/mysql.server restart
检验是否成功
登录mysql -u root -p
输入show variables like 'character%';
回车,显示
±-------------------------±---------------------------------+
| Variable_name | Value |
±-------------------------±---------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8
|
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8
|
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
±-------------------------±---------------------------------+
8 rows in set (0.01 sec)
设置远程连接mysql(设置后可以使用intellij idea连接)
use mysql;
select user,host from user;
update user set host='%' where user = 'root';
select user,host from user;
flush privileges;
其他命令
# 启动
sudo mysql.server start
# 重启
sudo mysql.server restart
# 停止
sudo mysql.server stop
# 检查 MySQL 运行状态
sudo mysql.server status
忘记密码
开一个终端
cd /usr/local/mysql/bin
sudo su
./mysqld_safe --skip-grant-tables &
重开一个终端
mysql
use mysql;
flush privileges;
set password for 'root'=password('新密码');
或者 set password for 'root'@'localhost'=password('新密码');
exit
mysql -u root -p