【MySQL】二进制安装MySQL

一、基于Ubuntu 二进制安装MySQL8.0 (5.7+适用)

1、创建用户

[root@Node-Ubuntu1804-20:~]# groupadd mysql
[root@Node-Ubuntu1804-20:~]# useradd -r -g mysql -s /usr/sbin/nologin mysql

 

2、创建目录

[root@Node-Ubuntu1804-20:~]# mkdir /data/mysql -p
[root@Node-Ubuntu1804-20:~]# chown -R mysql.mysql /data/mysql/

  

3、准备二进制程序

下载链接:https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.32-linux-glibc2.12-x86_64.tar

 

[root@Node-Ubuntu1804-20:~]# wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.32-linux-glibc2.12-x86_64.tar
[root@Node-Ubuntu1804-20:~]# tar xvf mysql-8.0.32-linux-glibc2.12-x86_64.tar
mysql-test-8.0.32-linux-glibc2.12-x86_64.tar.xz
mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
mysql-router-8.0.32-linux-glibc2.12-x86_64.tar.xz
[root@Node-Ubuntu1804-20:~]# tar xf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
[root@Node-Ubuntu1804-20:~]# cd /usr/local/
[root@Node-Ubuntu1804-20:/usr/local]# ln -sv mysql-8.0.32-linux-glibc2.12-x86_64 mysql
'mysql' -> 'mysql-8.0.32-linux-glibc2.12-x86_64'

  

4、安装依赖包

[root@Node-Ubuntu1804-20:~]# apt install -y libaio1 libaio-dev libncurses5-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-4.15.0-151 linux-headers-4.15.0-151-generic linux-image-4.15.0-151-generic linux-modules-4.15.0-151-generic
  linux-modules-extra-4.15.0-151-generic
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libtinfo-dev
Suggested packages:
  ncurses-doc
The following NEW packages will be installed:
  libaio-dev libaio1 libncurses5-dev libtinfo-dev
0 upgraded, 4 newly installed, 0 to remove and 71 not upgraded.
Need to get 275 kB of archives.
After this operation, 1510 kB of additional disk space will be used.
Get:1 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libaio1 amd64 0.3.110-5ubuntu0.1 [6476 B]
Get:2 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libaio-dev amd64 0.3.110-5ubuntu0.1 [12.8 kB]
Get:3 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libtinfo-dev amd64 6.1-1ubuntu1.18.04.1 [81.4 kB]
Get:4 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libncurses5-dev amd64 6.1-1ubuntu1.18.04.1 [174 kB]
Fetched 275 kB in 2s (123 kB/s)      
Selecting previously unselected package libaio1:amd64.
(Reading database ... 144068 files and directories currently installed.)
Preparing to unpack .../libaio1_0.3.110-5ubuntu0.1_amd64.deb ...
Unpacking libaio1:amd64 (0.3.110-5ubuntu0.1) ...
Selecting previously unselected package libaio-dev:amd64.
Preparing to unpack .../libaio-dev_0.3.110-5ubuntu0.1_amd64.deb ...
Unpacking libaio-dev:amd64 (0.3.110-5ubuntu0.1) ...
Selecting previously unselected package libtinfo-dev:amd64.
Preparing to unpack .../libtinfo-dev_6.1-1ubuntu1.18.04.1_amd64.deb ...
Unpacking libtinfo-dev:amd64 (6.1-1ubuntu1.18.04.1) ...
Selecting previously unselected package libncurses5-dev:amd64.
Preparing to unpack .../libncurses5-dev_6.1-1ubuntu1.18.04.1_amd64.deb ...
Unpacking libncurses5-dev:amd64 (6.1-1ubuntu1.18.04.1) ...
Setting up libtinfo-dev:amd64 (6.1-1ubuntu1.18.04.1) ...
Setting up libncurses5-dev:amd64 (6.1-1ubuntu1.18.04.1) ...
Setting up libaio1:amd64 (0.3.110-5ubuntu0.1) ...
Setting up libaio-dev:amd64 (0.3.110-5ubuntu0.1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.5) ...

  

5、设置PATH环境变量

[root@Node-Ubuntu1804-20:~]# echo "PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
[root@Node-Ubuntu1804-20:~]# . /etc/profile.d/mysql.sh 

 

6、创建配置文件

[root@Node-Ubuntu1804-20:~]# vim /etc/my.cnf
[root@Node-Ubuntu1804-20:~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
pid-file=/data/mysql/mysql.pid
log-error=/data/mysql/mysql.log

[client]
socket=/data/mysql/mysql.sock

  

7、创建数据库文件,并提取root密码

[root@Node-Ubuntu1804-20:~]# mysqld --initialize --user=mysql --datadir=/data/mysql
[root@Node
-Ubuntu1804-20:~]# grep password /data/mysql/mysql.log 2023-06-07T02:05:13.924955Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: k.k/j&XuT7uF [root@Node-Ubuntu1804-20:~]# awk '/temporary password/{print $NF}' /data/mysql/mysql.log k.k/j&XuT7uF

  

8、创建服务脚本,并启动服务

[root@Node-Ubuntu1804-20:~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@Node-Ubuntu1804-20:~]# /etc/init.d/mysqld start
[ ok ] Starting mysqld (via systemctl): mysqld.service.

  

9、修改初始密码,测试访问数据库

[root@Node-Ubuntu1804-20:~]# mysqladmin -uroot -p'k.k/j&XuT7uF' password 'mysql'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

[root@Node-Ubuntu1804-20:~]# mysql -uroot -pmysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.32 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

  

二、基于CentOS7  二进制安装MySQL8.0(5.7+适用)

1、创建用户

[root@node-centos7-70 ~]# groupadd mysql
[root@node-centos7-70 ~]# useradd -r -g mysql -s /usr/sbin/nologin mysql

 

2、创建目录

[root@node-centos7-70 ~]# mkdir /data/mysql -p
[root@node-centos7-70 ~]# chown -R mysql.mysql /data/mysql

 

3、准备二进制程序

[root@node-centos7-70 ~]# wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.32-linux-glibc2.12-x86_64.tar
--2023-06-07 10:54:29--  https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.32-linux-glibc2.12-x86_64.tar
正在解析主机 cdn.mysql.com (cdn.mysql.com)... 104.70.237.54
正在连接 cdn.mysql.com (cdn.mysql.com)|104.70.237.54|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1005660160 (959M) [application/x-tar]
正在保存至: “mysql-8.0.32-linux-glibc2.12-x86_64.tar100%[====================================================================================================>] 1,005,660,160 26.6MB/s 用时 29s    

2023-06-07 10:54:58 (32.8 MB/s) - 已保存 “mysql-8.0.32-linux-glibc2.12-x86_64.tar” [1005660160/1005660160])

[root@node-centos7-70 ~]# tar xvf mysql-8.0.32-linux-glibc2.12-x86_64.tar
mysql-test-8.0.32-linux-glibc2.12-x86_64.tar.xz
mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
mysql-router-8.0.32-linux-glibc2.12-x86_64.tar.xz
[root@node-centos7-70 ~]# tar xf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
[root@node-centos7-70 ~]# cd /usr/local
[root@node-centos7-70 local]# ln -sv mysql-8.0.32-linux-glibc2.12-x86_64 mysql
"mysql" -> "mysql-8.0.32-linux-glibc2.12-x86_64"

 

4、安装依赖包

[root@node-centos7-70 ~]# yum install -y libaio numactl-libs

 

5、设置PATH环境变量

[root@node-centos7-70 ~]# echo "PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
[root@node-centos7-70 ~]# . /etc/profile.d/mysql.sh

 

6、创建配置文件

[root@node-centos7-70 ~]# tee > /etc/my.cnf << EOF
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
pid-file=/data/mysql/mysql.pid
log-error=/data/mysql/mysql.log

[client]
socket=/data/mysql/mysql.sock
EOF

 

7、创建数据库文件,并提取root密码

[root@node-centos7-70 ~]# mysqld --initialize --user=mysql --datadir=/data/mysql
[root@node-centos7-70 ~]# grep password /data/mysql/mysql.log
2023-06-07T03:20:09.342795Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ,*9LyAr6wrr#
[root@node-centos7-70 ~]# awk '/temporary password/{print $NF}' /data/mysql/mysql.log 
,*9LyAr6wrr#

 

8、创建服务脚本,并启动服务

[root@node-centos7-70 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@node-centos7-70 ~]# chkconfig --add mysqld
[root@node-centos7-70 ~]# service mysqld start
Starting MySQL... SUCCESS! 

  

9、修改初始密码,测试访问数据库

[root@node-centos7-70 ~]# mysqladmin -uroot -p',*9LyAr6wrr#' password 'mysql'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@node-centos7-70 ~]# mysql -uroot -pmysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.32 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

 

三、shell 二进制一键安装 Mysql => 5.7+

#!/bin/bash
#
#********************************************************************
#Author:                janzen
#Date:                  2023-06-07
#FileName:             mysql_install.sh
#Description:          The test script
#Copyright (C):        2023 All rights reserved
#********************************************************************
ERR="echo -e \033[1;31m"
SUC="echo -e \033[1;32m"
WAR="echo -e \033[1;33m"
RED="\033[1;31m"
GRE="\033[1;32m"
YEL="\033[1;33m"
END="\033[m"

VERSION=$1
VERSION=${VERSION:-'8.0.32'}
MAIN_VERSION=`echo ${VERSION: 0:3}`
MYSQL="mysql-$VERSION-linux-glibc2.12-x86_64.tar"
URL="https://cdn.mysql.com/archives/mysql-$MAIN_VERSION/"
SRC='/tmp/'
MYSQL_DATA=/data/mysql
PASSWD='mysql'

os_type(){
grep centos /etc/os-release &> /dev/null && { echo "centos";return 0;} || grep ubuntu /etc/os-release &> /dev/null && { echo ubuntu; return 0;} || grep "Red Hat" /etc/os-release &> /dev/null && { echo "rhel";return 0; } || { echo "";return 1;}
}
wait_sec(){
        local ERR=${ERR:-"echo -e \e[1;31m"}
        local SUC=${SUC:-"echo -e \e[1;32m"}
        local WAR=${WAR:-"echo -e \e[1;33m"}
        local RED=${RED:-"\e[1;31m"}
        local GRE=${GRE:-"\e[1;32m"}
        local YEL=${YEL:-"\e[1;33m"}
        local END=${END:-"\e[m"}
        local sec=$1
        local STR=$2
        local STR=${STR:-"Waiting for "}
        local sec=${sec:-10}
        local mark='>'
        
        $WAR"$STR($sec s) ......"$END
        for i in `eval echo {1..$sec}`;do
                printf "$YEL Waiting: [%-${sec}s]%ds\r $END" "${mark}" "${i}"
                mark=">${mark}"
                sleep 1
        done
        echo
}

check() {
	mysql -V &> /dev/null || mysqld -V &> /dev/null && { $ERR"Mysql 已安装,程序退出"$END; exit 1;}
	[ $UID -ne 0 ] && { $ERR"当前用户非root用户,请切换用户后重试"$END; exit 1;}
	[ -e /usr/local/mysql ] && { $ERR"Mysql 已存在,程序退出"$END; exit 1;}
	[ -e "$SRC$MYSQL.xz" ] || [ -e "$SRC$MYSQL.gz" ] && $WAR"检测到程序包已存在 `ls $SRC$MYSQL*`"$END || DOWNLOAD=1
	$SUC"初始环境检查完成"$END
	echo
}

install_mysql() {
	wait_sec $1 $2
	if [ $DOWNLOAD ]; then
		$WAR"本地安装程序不存在,开始联网下载程序包"$END
		wget -P $SRC "$URL$MYSQL.xz" || wget -P $SRC "$URL$MYSQL.gz" || { $ERR"无法连接目标地址"$END;exit 1; }
	fi
	tar xf "$SRC$MYSQL.xz" -C /usr/local/ &> /dev/null || tar xf "$SRC$MYSQL.gz" -C /usr/local/ &> /dev/null || { $ERR"未找到目标安装包"$END;exit 1; }
	MYSQL_DIR=`echo -e "$MYSQL" | sed -nr 's/(^.*)\.tar*/\1/p'`
	cd /usr/local
	[ -e $MYSQL_DIR ] && ln -sv $MYSQL_DIR mysql || { $ERR$MYSQL_DIR" not exists"$END;exit 1; }
	id mysql > /dev/null || useradd -s /sbin/nologin -r mysql
	mkdir -p $MYSQL_DATA
	chown mysql.mysql $MYSQL_DATA
	echo "PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
	chmod a+x /etc/profile.d/mysql.sh
	source /etc/profile.d/mysql.sh
	export "PATH=/usr/local/mysql/bin:$PATH"

	if [ `os_type` == 'centos' ]; then
		yum install -y libaio numactl-libs
	elif [ `os_type` == 'ubuntu' ]; then
		apt install -y libaio1 libaio-dev libncurses5-dev || { rm -f {/var/lib/apt/lists/lock,/var/cache/apt/archives/lock,/var/lib/dpkg/lock*}; dpkg --configure -a; sleep 5; apt install -y libaio1 libaio-dev libncurses5-dev; } && $SUC"安装依赖包成功"$END || { $ERR"安装依赖包失败"$END; exit 1;}
	fi

	tee > /etc/my.cnf << EOF
[mysqld]
datadir=$MYSQL_DATA
socket=$MYSQL_DATA/mysql.sock
pid-file=$MYSQL_DATA/mysql.pid
log-error=$MYSQL_DATA/mysql.log

[client]
socket=$MYSQL_DATA/mysql.sock
auto-rehash
[mysqladmin]
socket=$MYSQL_DATA/mysql.sock
EOF
	cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
	mysqld --initialize --user=mysql --datadir=$MYSQL_DATA
	DEFT_PASSWD=`awk '/temporary password/{print $NF}' $MYSQL_DATA/mysql.log`
	/etc/init.d/mysqld start
	if [ `os_type` == 'centos' ]; then
		chkconfig --add mysqld
		chkconfig on
	elif [ `os_type` == 'ubuntu' ]; then
		tee >> /lib/systemd/system/rc-local.service << EOF
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
EOF
		tee > '/etc/rc.local' << EOF
#!/bin/bash                                                                                                                                     
/etc/init.d/mysqld start
EOF
		chmod a+x /etc/rc.local
		ln -sf /lib/systemd/system/rc-local.service  /etc/systemd/system/rc-local.service 
		systemctl daemon-reload
		systemctl restart rc-local.service
	fi
	mysqladmin -uroot -p"$DEFT_PASSWD" password "$PASSWD"
	mysql -uroot -p"$PASSWD" -e "show databases;" &> /dev/null && $SUC"MySQL数据库运行成功"$END || { $ERR"MySQL数据库运行失败"$END;return 1; }
}

check
install_mysql 10 "即将开始安装数据库"

 

posted @ 2023-06-08 13:56  Janzen_Q  阅读(478)  评论(1编辑  收藏  举报