Mac上使用Homebrew进行MySQL卸载、安装,解决乱码
Mac上使用Homebrew进行MySQL卸载
系统环境
$ brew remove mysql@5.6
$ brew cleanup
$ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql@5.6.plist
删除文件
$ sudo rm -f /etc/my.cnf
$ sudo rm -rf /usr/local/var/mysql
$ sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql@5.6.plist
Mac上使用Homebrew安装MySQL
$ brew install mysql@5.6
让 MySQL 开机自行启动
$ ln -sfv /usr/local/opt/mysql@5.6/*.plist ~/Library/LaunchAgents
$ sudo find /usr/local/Cellar/mysql@5.6/5.6.36_1/ -name "homebrew.mxcl.mysql@5.6.plist" -exec cp {} ~/Library/LaunchAgents/ \;
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql@5.6.plist
指定MySQL数据存放路径
$ mysql_install_db --verbose --user=`whoami` --basedir="/usr/local/Cellar/mysql@5.6/5.6.36_1" --datadir=/usr/local/var/mysql --tmpdir=/tmp
$ cd . ; /usr/local/Cellar/mysql@5.6/5.6.36_1/bin/mysqld_safe &
配置MySQL
修改 /etc/my.cnf,没有就从MySQL安装目录拷贝一个模板过来。
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
port=3306
datadir=/usr/local/var/mysql
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
将mysql加入系统环境变量
执行vim ~/.bash_profile
在该文件中添加如下语句,完成后,按esc,然后输入wq保存。
export PATH=/usr/local/opt/mysql@5.6/bin:$PATH
最后在命令行输入source ~/.bash_profile
ok ,可以启动MySQL了。
$ mysql.server start
权限配置参见官网:
https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
设置密码
默认是没有密码的,进去mysql后,执行语句:
mysql> set password for 'root'@'localhost'=password('root');
Query OK, 0 rows affected (0.01 sec)
接下来使用密码登录MySQL:
$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 395
Server version: 5.6.36 Homebrew
Copyright (c) 2000, 2017, 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> 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/Cellar/mysql@5.6/5.6.36_1/share/mysql/charsets/ |
+--------------------------+------------------------------------------------------------+
8 rows in set (0.04 sec)
看到utf8,编码就不会出问题了。
结语
之所以写这篇,原因是从Windows转到Mac环境搞开发,碰到了乱码问题,各种配置都试了也没解决。卸载重装妥妥的。