Mac下MySQL安装

288 阅读3分钟

一、说明

在写这边文章之前我的mysql是去官网下载dmg文件进行安装,然后在.bash_profile中进行环境配置的。最近使用的时候经常报mysql command not found(之前不是很常用mysql就一直这样了,尝试了一些方法进行配置还是会偶尔出现这样的问题)。最近在学习python后台开发(后面会有文章更新)所以会经常使用到mysql,因此有了这篇文章;

二、MySQL的卸载

  • 首先在系统偏好设置中暂停mysql的服务,依次执行下面的指令
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /var/db/receipts/com.mysql.*
  • 我在执行其中一条指令的时候遇到一个问题,参考了这篇文章解决了https://blog.csdn.net/rbtoooooooooo/article/details/73927397

三、MySQL的安装

  • 前提安装homebrew(不知道安装的出门左转:www.baidu.com)
  • 安装指令
brew install mysql
  • 配置指令:一些基本设置:如密码
mysql_secure_installation
  • 详细配置按照下面的示例进行操作:
cometdeMacBook-Pro:~ comet
$ mysql_secure_installation
 
Securing the MySQL server deployment.
 
Connecting to MySQL using a blank password.
 
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
 // 这个选yes的话密码长度就必须要设置为8位以上,但我只想要6位的
Press y|Y for Yes, any other key for No: N   
Please set the password for root here.
 
New password:            // 设置密码
 
Re-enter new password:     // 再一次确认密码
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
 // 移除不用密码的那个账户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y    
Success.
 
 
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
 //是否禁止远程登录
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
 
 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
 
 // 是否删除test库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.
 
 - Removing privileges on test database...
Success.
 
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
 
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
 
All done!

  • 现在配置完成了说一下常用的指令
登陆mysql:mysql -u root -p
启动mysql:brew services start mysql@5.7
停止mysql:brew services stop mysql@5.7
  • mysql的简单教程推荐:菜鸟教程进行学习;
  • mysql与相应的语言的学习:www.baidu.com;

最后:说一点学习感悟

  1. 选择视频学习还是文本(图书活着其他文字)学习
    • 说明:不管是那种方法进行学习有利也有弊,重要的是要掌握学习的方法,这里就不详细说了,后面我会有一篇专门的文章进行阐述;
  2. 文章中很多都是前人的经验,非常感谢!每个人遇到的问题可能不一样,但希望可以帮助到你。走过的每个坑,回头看时会发现最深的那个坑最坑人了(哈哈哈),既然看到了这篇文章,我们就是坑友了。有兴趣的加个微信:15870835056;