mac上mysql.tar.gz的安装图解

本文详述了在Mac macOS Mojave上安装MySQL 5.6、5.7和8.0的步骤,包括使用shell脚本进行安装、配置环境变量、设置开机自启动、修改默认密码、创建数据库、备份数据库等操作。提供了各版本的安装脚本及使用说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要讲述mac下5.6/5.7/8.0用tar.gz方式安装的mysql数据库,并晒出简单的shell脚本和执行方式,自测可以通过

主要功能点:

mysql-install.sh:安装,配置环境变量,开机自启动,修改默认密码

mysql-config.sh:建库,使用sql文件还原,创建用户和授权

mysql-backup.sh:备份,清楚历史备份

版本:

mac---macOS Mojave 10.14 18A391 Lazy Installer.cdr

mysql 5.6---5.6.33

mysql 5.7---5.7.31

mysql 8.0---8.0.22

使用说明:

1-下载下来的tar.gz文件需要先解压,然后重新命名为mysql.tar.gz

2-5.7和8.0版本需要预先在mysql文件夹下创建data的文件夹

3-解压脚本:

例如5.7版本的tar.gz文件:

解压到tar.gz包的同级目录下:

 sudo tar xvf mysql-5.7.31-macos10.14-x86_64.tar.gz

修改文件夹名称:

sudo mv mysql-5.7.31-macos10.14-x86_64 mysql

如果5.7和8.0版本需要在mysql文件夹下创建data目录

切入mysql文件夹下

sudo mkdir data

注意:切到mysql的同级目录下,然后压缩文件夹

sudo tar cvf mysql.tar.gz mysql

4-下载

https://downloads.mysql.com/archives/community/

5.6

5.7

8.0

5-文件列表

mysql-install.sh:安装脚本,需要com.oracle.oss.mysql.mysqld.plistmy.cnfmysql.tar.gz等文件(mysql.tar.gz为安装文件,my.cnf为配置文件,com.oracle.oss.mysql.mysqld.plist为开机自启动脚本)

执行方式:

sudo ./mysql-install.sh

执行结果:

mysql-config.sh:配置文件,需要sql文件(.sql文件为还原数据库的sql,不包括创建用户和授权)

执行方式:

sudo ./mysql-config.sh

执行结果:

mysql-backup.sh:备份文件,备份结果在该脚本所在目录

执行方式:

sudo ./mysql-backup.sh

执行结果:

脚本放送:

 

5.6-mysql-install.sh

#!/bin/bash

#the parameters
_install_path=/usr/local

_mysql_path=/usr/local/mysql

_my_path=/etc

_root_password=ABCDEFG

_cur_path=$(cd $(dirname $0); pwd)

_comp_file=$_cur_path/mysql.tar.gz

_my_file=$_cur_path/my.cnf

_install_log=$_cur_path/install_log.txt

_bin_path=$_mysql_path/bin

_self_start_dir=/Library/LaunchDaemons

_self_start_file=com.oracle.oss.mysql.mysqld.plist 
###############################

#decompression the file
tar vxf $_comp_file -C $_install_path

if [ ! $? -eq 0 ]; then
	echo 'Unzip failed!'>>$_install_log
	echo 'Unzip failed!'
	exit 1	
fi

cd $_mysql_path
chown -R _mysql .
chgrp -R _mysql .

#put the my.cnf to 
cp $_my_file $_mysql_path/my.cnf

if [ ! $? -eq 0 ]; then
	echo 'Fail to copy my.cnf!'>>$_install_log
	echo 'Fail to copy my.cnf!'
	exit 3	
fi

##init mysql
./scripts/mysql_install_db --user=_mysql --basedir=$_mysql_path --datadir=$_mysql_path/data
if [ ! $? -eq 0 ]; then
	echo 'Install Failed!'>>$_install_log
	echo 'Install Failed!'
	exit 5	
fi

##set path environment
echo export PATH=${PATH}:/usr/local/mysql/bin >> ~/.bash_profile
echo source ~/.bash_profile >> ~/.zshrc
source ~/.bash_profile

if [ ! $? -eq 0 ]; then
	echo 'Set Environment Path Failed!'>>$_install_log
	echo 'Set Environment Path Failed!'
	exit 6	
fi

##start mysql
$_mysql_path/support-files/mysql.server start

if [ ! $? -eq 0 ]; then
	echo 'Start mysql server Failed!'>>$_install_log
	echo 'Start mysql server Failed!'
	exit 7	
fi

cp $_cur_path/$_self_start_file $_self_start_dir
launchctl load -w $_self_start_dir/$_self_start_file

if [ ! $? -eq 0 ]; then
	echo 'Set self-starting Failed!'>>$_install_log
	echo 'Set self-starting Failed!'
	exit 8	
fi

##modify the password of root
./bin/mysqladmin -u root password $_root_password

if [ ! $? -eq 0 ]; then
	echo 'Fail to reset the password of root!'>>$_install_log
	echo 'Fail to reset the password of root!'
	exit 9
fi

mysql -u root -p$_root_password << EOF
        drop user ''@'localhost';
	drop user root@'::1';
	drop user root@127.0.0.1;
	GRANT all privileges ON *.* TO root@"%" IDENTIFIED BY '$_root_password' with grant option;
EOF

if [ ! $? -eq 0 ]; then
	echo 'Fail to clear the unuse user!'>>$_install_log
	echo 'Fail to clear the unuse use
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值