brew安装mysql

  1. 1.使用brew安装mysql
  2. brew install mysql

启动

  1. 2.启动mysql服务
  2. 方式1: brew services start mysql (推荐)
  3. 方式2: mysql.server start
  4. #重启
  5. 方式1: brew services restart mysql
  6. 方式2: mysql.server restart
  7. #配置launchctl自启动
  8. 方式1:
  9. mkdir -p ~/Library/LaunchAgents
  10. ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
  11. find /usr/local/Cellar/mysql/ -name "homebrew.mxcl.mysql.plist" -exec cp {} ~/Library/LaunchAgents/ \;
  12. launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  13. 方式2:
  14. ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents/
  15. launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

初始化

  1. mysql_secure_installation

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG 请输入0=低,1=中,2=强

关闭与卸载

  1. #关闭停止
  2. 方式1: mysql.server stop
  3. 方式2: brew services stop mysql
  4. #卸载
  5. brew uninstall mysql
  6. #彻底清除残支
  7. sudo rm /usr/local/mysql
  8. sudo rm -rf /usr/local/mysql*
  9. sudo rm -rf /Library/StartupItems/MySQLCOM
  10. sudo rm -rf /Library/PreferencePanes/My*
  11. edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
  12. rm -rf ~/Library/PreferencePanes/My*
  13. sudo rm -rf /Library/Receipts/mysql*
  14. sudo rm -rf /Library/Receipts/MySQL*
  15. sudo rm -rf /private/var/db/receipts/*mysql*

查看

  1. #查看状态
  2. mysql.server status ps -ef|grep mysqld
  3. #查看所有可由Homebrew管理的服务
  4. brew services list
  5. #查看帮助
  6. mysql --help mysql -?

创建my.cnf配置文件

  • 首先需要知道系统是按如下顺序去找my.cnf:

i. /etc/my.cnf
ii. /etc/mysql/my.cnf
iii. /usr/local/etc/my.cnf
iv. ~/.my.cnf

[client] default-character-set=utf8

password = your_password

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’

Don’t listen on a TCP/IP port at all. This can be a security enhancement,

skip-networking

log-bin=mysql-bin

log-bin=/var/log/mysql/mysql-bin

binlog_format=mixed

server-id = 1

复制从机(注释掉主部分以使用此功能)

第一种方法:

使用CHANGE MASTER TO命令(在我们的手册中有详细描述)-语法为:

CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,

MASTER_USER=, MASTER_PASSWORD= ;

Example:

CHANGE MASTER TO MASTER_HOST=’125.564.12.1’, MASTER_PORT=3306,

MASTER_USER=’joe’, MASTER_PASSWORD=’secret’;

第二种方法:

如果设置了主主机,则默认为2

server-id = 2

#

此从机的复制主机-必需

master-host =

连接时从属服务器将用于身份验证的用户名,to the master - required

master-user =

从机连接到时将使用的密码,to the master - required

master-password =

主机正在监听的端口,可选-默认为3306

master-port =

二进制日志记录-不需要从服务器,但建议

log-bin=mysql-bin

如果使用InnoDB表,请取消注释以下内容

innodb_data_home_dir = /usr/local/mysql/data

innodb_data_file_path = ibdata1:10M:autoextend

innodb_log_group_home_dir = /usr/local/mysql/data

innodb_buffer_pool_size = 16M

innodb_additional_mem_pool_size = 2M

innodb_log_file_size = 5M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 1

innodb_lock_wait_timeout = 50

[mysqldump] quick max_allowed_packet = 16M

[mysql] no-auto-rehash

Remove the next comment character if you are not familiar with SQL

safe-updates

default-character-set=utf8

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

[mysqlhotcopy] interactive-timeout

  1. <a name="MWzlp"></a>
  2. ### 简易安装
  3. <a name="DXfpa"></a>
  4. #### 下载安装包
  5. > 首先去官网下载mac版的mysql,链接如下:[MySql下载](https://dev.mysql.com/downloads/mysql/)
  6. <a name="eZyTR"></a>
  7. #### 设置全局变量
  8. ```bash
  9. #设置暂时性全局变量
  10. export PATH=$PATH:/usr/local/git/bin
  11. #设置永久性全局变量 vim ~/.bash_profile 或者 vim /etc/profile
  12. PATH=$PATH:/usr/local/mysql/bin

使用永久性全局变量别忘了使文件生效 source ~/.bash_profile 或 source /etc/profile

连接远程

  1. mysql -h 119.23.72.172 -uroot -pQwer@1234

问题

警告:全局可写配置文件’/etc/mysql/my.cnf’被忽略

[Warning] World-writable config file ‘/usr/local/etc/my.cnf’ is ignored.

  1. chmod 644 /etc/my.cnf && chmod 644 /usr/local/etc/my.cnf

用户“root”@“localhost”的访问被拒绝(使用密码:否)

Access denied for user ‘root’@’localhost’ (using password: NO)

  1. 1、先停掉原来的服务
  2. 方法一: brew services stop mysql
  3. 方法二: kill -9 <pid>
  4. 2、使用安全模式登陆,跳过密码验证
  5. 方法一(推荐):使用一个窗口运行下面这行代码,然后打开【新的shell窗口】操作步骤3
  6. mysqld_safe --user=mysql --skip-grant-tables --skip-networking&
  7. 方法二:vim /etc/my.cnf ,在配置文件中加入下面这行代码,保存并重启mysql服务,然后打开【新的shell窗口】操作步骤3
  8. skip-grant-tables
  9. 3、进入mysql,修改密码:
  10. mysql -u root -p #连接mysql服务,不要输入密码,两次回车进入
  11. select version(); #查看版本号
  12. flush privileges; #刷新权限
  13. alter user'root'@'localhost' IDENTIFIED BY 'aBc@123456'; # 注意: mysql8.0以上密码策略限制必须要大小写加数字特殊符号
  14. flush privileges; #再次刷新权限
  15. 4.重启mysql登陆测试
  16. 按「步骤1方法二」需要: 把添加的 skip-grant-tables 删除或注释
  17. brew services start mysql

Failed to start mysqld.service: Unit mysqld.service not found.

启动mysqld.service失败:找不到命令行mysqld.service,安装没有成功

  1. mysql -h 119.23.72.172 -uroot -pQwer@1234