mac mysql安装

场景:本地搭建服务需要安装mysql数据库

 

参考:https://blog.csdn.net/lrglgy/article/details/90549541

下载地址:

https://dev.mysql.com/downloads/mysql/

 

1、卸载mysql

如果之前安装过mysql的话,建议先按照这个链接先去检查是否卸载干净

https://www.jianshu.com/p/276c1271ae14

2、homebrew安装mysql

一、安装mysql
$ brew install mysql



二、配置自启动
$ mkdir -p ~/Library/LaunchAgents
$ ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
$ find /usr/local/Cellar/mysql/ -name "homebrew.mxcl.mysql.plist" -exec cp {} ~/Library/LaunchAgents/ \;
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist


三、修改mysql密码
1.先启动mysql服务
$ mysql.server start

2.继续执行mysql_secure_installation


四、创建配置文件
1.首先需要知道系统是按如下顺序去找my.cnf:
i.    /etc/my.cnf
ii.   /etc/mysql/my.cnf
iii.  /usr/local/etc/my.cnf
iv.  ~/.my.cnf
2.所以就在/etc下创建my.cnf
$ cd /etc
$ sudo vim my.cnf

 

[client]
default-character-set=utf8
port = 3306
socket = /tmp/mysql.sock
[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
character-set-server=utf8
secure_file_priv=''
init_connect='SET NAMES utf8'
log-bin=mysql-bin
binlog_format=mixed
server-id = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
default-character-set=utf8

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

  

 

Last login: Thu Feb  4 17:30:37 on ttys001
gina.qi@ginaqideMacBook-Pro ~ % brew install mysql
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/mysql-8.0.22_1.catalina.bottle.tar.gz
Already downloaded: /Users/gina.qi/Library/Caches/Homebrew/downloads/94ac1cfe41ac356c4e573b8b56689d2257043c9984558c69a7ce89ef91b58702--mysql-8.0.22_1.catalina.bottle.tar.gz
==> Pouring mysql-8.0.22_1.catalina.bottle.tar.gz
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mysql/8.0.22_1: 294 files, 296.5MB
gina.qi@ginaqideMacBook-Pro ~ %

  

配置自启动

gina.qi@ginaqideMacBook-Pro ~ % cd /usr/local/opt
gina.qi@ginaqideMacBook-Pro opt % ls
gettext		libunistring	nvm		openssl@1.1	redis@6.0
java		lrzsz		openjdk		protobuf	wget
kafka		mysql		openjdk@15	protobuf@3.14	zookeeper
libidn2		mysql@8.0	openssl		redis
gina.qi@ginaqideMacBook-Pro opt % cd mysql
gina.qi@ginaqideMacBook-Pro mysql % ls
INSTALL_RECEIPT.json		README-test			include
LICENSE				README.router			lib
LICENSE-test			bin				mysqlrouter-log-rotate
LICENSE.router			docs				share
README				homebrew.mxcl.mysql.plist	support-files
gina.qi@ginaqideMacBook-Pro mysql % ls /Library/LaunchAgents
CGEData.agent.plist			com.sangfor.ECAgentProxy.plist
com.oracle.java.Java-Updater.plist
gina.qi@ginaqideMacBook-Pro mysql % ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
/Users/gina.qi/Library/LaunchAgents/homebrew.mxcl.mysql.plist -> /usr/local/opt/mysql/homebrew.mxcl.mysql.plist
gina.qi@ginaqideMacBook-Pro mysql % fin /usr/local/Cellar/mysql -name "homebrew.mxcl.mysql.plist" -exec cp {} ~/Library/LaunchAgents \;
zsh: command not found: fin
gina.qi@ginaqideMacBook-Pro mysql % find /usr/local/Cellar/mysql -name "homebrew.mxcl.mysql.plist" -exec cp {} ~/Library/LaunchAgents \;
cp: /Users/gina.qi/Library/LaunchAgents/homebrew.mxcl.mysql.plist and /usr/local/Cellar/mysql/8.0.22_1/homebrew.mxcl.mysql.plist are identical (not copied).
gina.qi@ginaqideMacBook-Pro mysql % launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
/Users/gina.qi/Library/LaunchAgents/homebrew.mxcl.mysql.plist: service already loaded
gina.qi@ginaqideMacBook-Pro mysql %

  

修改mysql密码,密码:000000

gina.qi@ginaqideMacBook-Pro mysql % mysql.server start
Starting MySQL
 SUCCESS!
gina.qi@ginaqideMacBook-Pro mysql % 2021-02-04T09:44:38.6NZ mysqld_safe A mysqld process already exists

gina.qi@ginaqideMacBook-Pro mysql % mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT 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 component?

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.


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!
gina.qi@ginaqideMacBook-Pro mysql %

  

常见操作

启动命令:sudo mysql.server start
关闭命令:sudo mysql.server stop
查看是否已经启动了:sudo mysql.server status

 

数据库操作:

https://www.jianshu.com/p/d0e1e3b26ca4

进入命令行:mysql -hlocalhost -uroot -p

 

 

 

posted @ 2022-01-22 11:49  gina_qq  阅读(142)  评论(0)    收藏  举报