安装有多种方法,如二进制安装,编译安装,rpm包的安装。本文主要讲解MySQL的二进制安装,安装流程更简单清晰,使用一键安装脚本,使安装更容易。
一、官网选择下载版本
进入www.mysql.com的官网,进入社区版的下载,具体下载界面的网址为:MySQL :: Download MySQL Community Server (Archived Versions)
按照个人需求对版本进行选择,本文以linux为Centos7的版本为例进行演示。
按照图中所示,对下载版本进行选择,并点击下载。
二、将MySQL安装至linux
1.上传文件
建议在新建的虚拟机中安装MySQL,确保环境干净,不容易出错。
使用Xftp或其他文件传输软件,将下载好的MySQL安装包传输至需要安装MySQL的虚拟机中:
在linux中进入家目录,查看文件是否传输成功,若能看到MySQL的压缩包,则可进行下一步操作。
[root@localhost ~] ls
anaconda-ks.cfg mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz
2.一键安装脚本
代码如下
[root@localhost ~]# vim onekey_install_mysql_binary_v3.sh
echo 'PATH=/usr/local/mysql/bin:$PATH' >>/root/.bashrc
#复制support-files里的mysql.server文件到/etc/init.d/目录下叫mysqld
cp ../support-files/mysql.server /etc/init.d/mysqld
#修改/etc/init.d/mysqld脚本文件里的datadir目录的值
sed -i '70c datadir=/data/mysql' /etc/init.d/mysqld
#生成/etc/my.cnf配置文件
cat >/etc/my.cnf <<EOF
[mysqld_safe]
[client]
socket=/data/mysql/mysql.sock
[mysqld]
socket=/data/mysql/mysql.sock
port = 3306
open_files_limit = 8192
innodb_buffer_pool_size = 512M
character-set-server=utf8mb4
[mysql]
auto-rehash
prompt=\\u@\\d \\R:\\m mysql>
EOF
#修改内核的open file的数量
ulimit -n 1000000
#设置开机启动的时候也配置生效
echo "ulimit -n 1000000" >>/etc/rc.local
chmod +x /etc/rc.d/rc.local
#启动mysqld进程
service mysqld start
#将mysqld添加到linux系统里服务管理名单里
/sbin/chkconfig --add mysqld
#设置mysqld服务开机启动
/sbin/chkconfig mysqld on
#初次修改密码需要使用--connect-expired-password 选项
#-e 后面接的表示是在mysql里需要执行命令 execute 执行
#set password='123456#'; 修改root用户的密码为123456#
mysql -uroot -p$tem_passwd --connect-expired-password -e "set password='123456#';"
#检验上一步修改密码是否成功,如果有输出能看到mysql里的数据库,说明成功。
mysql -uroot -p'123456#' -e "show databases;"
其中用户和密码可以自行修改,安装完后,使用命令 su - root重新切换一下用户,使用命令
ps aux|grep mysqld查看mysqld是否启动:
再按照所设置的密码登录MySQL,若显示为以下界面,则表示安装配置成功,可以正常使用。
[root@localhost ~] mysql -uroot -p'Sanchuang123#'
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 20
Server version: 5.7.40 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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。