目录
🍁rpm方式安装
🍀下载软件包
🍀前置配置
🍀安装MySQL
🍁yum方式安装
🍀下载软件包
🍀安装MySQL
🍁二进制方式安装
🍀下载软件包
🍀安装MySQL
🍁编译方式安装
🍀下载软件包
🍀安装编译工具
🍀安装MySQL
🍁总结四种安装方式
🦐博客主页:大虾好吃吗的博客
🦐MySQL专栏:MySQL专栏地址
如何学会一门it技术?刚开始接触的一门新的课程时,都会有点发怵的,但是等你学完了,回头细想,也没什么难得嘛。如果学习MySQL课程时,除了理论知识外,最重要的就是把MySQL服务搭建出来,学会本章,你会发现没有什么难的,无他唯熟尔。如果想了解MySQL如何使用,点MySQL专栏 如果想了解更多运维知识,点我博客主页 学无止境,让我们一起学习下去吧。
rpm方式安装
下载软件包
既然要安装,就需要有安装包,我们先从下载安装包开始去下载,注意自己的主机和版本,我这里用的是Centos7.9
操作步骤(附图):官网下载地址→MySQLrpm包官网下载
前置配置
下载后创建一个存放rpm包目录,或使用已有的目录,并检查系统内是否有自带的mariadb服务,有则卸载没有就忽略。
[root@daxia ~]# mkdir /rpmdir
[root@daxia ~]# ls /rpmdir/
mysql-community-client-5.7.40-1.el7.x86_64.rpm mysql-community-libs-5.7.40-1.el7.x86_64.rpm
mysql-community-common-5.7.40-1.el7.x86_64.rpm mysql-community-server-5.7.40-1.el7.x86_64.rpm
[root@daxia ~]# rpm -qa |grep mariadb
[root@daxia ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
安装MySQL
安装,两个参数分别表示强制安装和忽略依赖
[root@daxia ~]# rpm -ivh /rpmdir/* --nodeps --force
[root@daxia ~]# rpm -qa |grep mysql
mysql-community-client-5.7.40-1.el7.x86_64
mysql-community-server-5.7.40-1.el7.x86_64
mysql-community-common-5.7.40-1.el7.x86_64
mysql-community-libs-5.7.40-1.el7.x86_64
查看版本,启动mysql,登录发现要密码, 去查看log日志。最后查看到密码是D5r-mlkCMuIf
[root@daxia ~]# mysql --version
mysql Ver 14.14 Distrib 5.7.40, for Linux (x86_64) using EditLine wrapper
[root@daxia ~]# systemctl start mysqld
[root@daxia ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@daxia ~]# grep password /var/log/mysqld.log
2023-06-05T00:21:35.815624Z 1 [Note] A temporary password is generated for root@localhost: D5r-mlkCMuIf
2023-06-05T00:21:39.800941Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)
创建密码,第一次输入日志中的临时密码,第二次输入新密码(符合密码复杂性要求8位以上大小写特殊字符)。
[root@daxia ~]# mysqladmin -uroot -p password
Enter password: #临时密码
New password: #新密码
yum方式安装
下载软件包
指定官网源,安装mysql。
[root@daxia ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
#省略安装部分
[root@daxia ~]# yum -y install mysql57-community-release-el7-11.noarch.rpm
[root@daxia ~]# yum install -y mysql-community-server --nogpgcheck
安装MySQL
启动mysql,登录时发现需要密码,这时我们还是去看日志文件中的临时密码,登录即可。符合密码复杂性要求8位以上大小写特殊字符。
[root@daxia ~]# systemctl start mysqld
[root@daxia ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@daxia ~]# grep password /var/log/mysqld.log
2023-06-05T00:59:00.492033Z 1 [Note] A temporary password is generated for root@localhost: el55BgQwll/t
2023-06-05T00:59:14.381115Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)
[root@daxia ~]# mysqladmin -uroot -p password
Enter password: #临时密码
New password: #新密码
可以通过修改安全策略的方式,只校验密码长度(8位)
mysql> set global validate_password_policy=0
-> ;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user root@localhost identified by '12345678';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@daxia ~]# mysql -uroot -p12345678
二进制方式安装
下载软件包
官网下载地址二进制包官网下载
安装MySQL
二进制安装这里就使用脚本了,注意查看脚本注意事项。虽然我使用的是脚本,但还是强烈建议自行编辑命令,脚本写的很简单,仅用来二进制MySQL也够了,如果学习脚本请参考Linux入门到精通专栏。
[root@localhost /]# mkdir /sh
[root@localhost /]# cd /sh
[root@localhost sh]# vim mysql.sh
[root@localhost sh]# mv /mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz /usr/src
[root@localhost sh]# sh mysql.sh
#!/bin/bash
#把mysql安装包放到/usr/src/目录下。
#二进制安装MySQL,请使用source执行脚本否则环境变量不生效(密码为$pass,默认为登录状态)。
#登录mysql后密码修改命令:alter user root@localhost identified by '密码';
#解压mysq-5.7.40
tar zxf /usr/src/mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
mv /usr/local/mysql-5.7.40-linux-glibc2.12-x86_64 /usr/local/mysql
#删除冲突软件
rpm -e mariadb-libs --nodeps
#创建/data目录并赋权
mkdir -p /usr/local/mysql/data
groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql
chown -R mysql:mysql /usr/local/mysql/
#编写my.cnf配置文件
cat >> /etc/my.cnf << end
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
pid-file=/usr/local/mysql/data/mysql.pid
log_error=/usr/local/mysql/data/mysql.log
socket=/tmp/mysql.sock
end
#启动脚本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#设置环境变量
sed -i '2i export PATH=\$PATH:/usr/local/mysql/bin' /etc/profile
source /etc/profile
#初始化mysql
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
#启动mysql
/etc/init.d/mysqld start
#systemctl管理
cat >> /etc/systemd/system/mysqld.service << end
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
end
systemctl restart mysqld
#查看mysql初始密码并登录
pass=$(grep password /usr/local/mysql/data/mysql.log | grep 'password is' | awk '{print $NF}')
export pass
mysql -uroot -p$pass
[root@localhost sh]# sh mysql.sh
执行后自动登录MySQL,需要修改密码。
直接在里面修改密码,修改后退出。
mysql> alter user root@localhost identified by '123';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
测试登录,我直接登录时发现登录不了,想了下哪里出问题了,如果没安装成功也不可能执行脚本后就自动登录到MySQL里面。查看了下位置变量/etc/profile发现路径也插入成功了,再次执行source后就可以登录了。
[root@localhost sh]# mysql -uroot -p123
bash: mysql: command not found...
[root@localhost sh]# source /etc/profile
[root@localhost sh]# mysql -uroot -p123
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 4
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
编译方式安装
下载软件包
编译安装MySQL前需要先安装编译工具和编译版本的MySQL包。下载网址如下,根据自身需求安装对应的版本即可。
cmake-3.26.4.tar.gz Download | CMake
ncurses-5.9.tar.gz ftp://ftp.gnu.org/gnu/ncurses/
bison-3.8.1.tar.gz Index of /gnu/bison
mysql-5.7.40.tar.gz http://cdn.mysql.com/Downloads/MySQL-5.7/mysql-5.7.40.tar.gz
Boost_1_59_0.tar.gz http://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
下载后直接把五个包放到/usr/local/src下
[root@daxia ~]# cd /usr/local/src
[root@daxia src]# ls
bison-3.8.1.tar.gz boost_1_59_0.tar.gz cmake-3.26.4.tar.gz mysql-5.7.40.tar.gz ncurses-5.9.tar.gz
安装编译工具
[root@daxia src]# tar zxf cmake-3.26.4.tar.gz
[root@daxia src]# cd cmake-3.26.4/
[root@daxia cmake-3.26.4]# ./bootstrap && gmake && gmake install #执行这个命令报错,如果报错就执行下面命令下载ssl然后在安装
[root@daxia cmake-3.26.4]# yum install -y openssl openssl-devel
[root@daxia cmake-3.26.4]# cd /usr/local/src
[root@daxia src]# tar zxf ncurses-5.9.tar.gz
[root@daxia src]# cd ncurses-5.9/
[root@daxia ncurses-5.9]# ./configure && make && make install
[root@daxia ncurses-5.9]# cd /usr/local/src/
[root@daxia src]# tar zxf bison-3.8.1.tar.gz
[root@daxia src]# cd bison-3.8.1/
[root@daxia bison-3.8.1]# ./configure && make &&make install
[root@daxia bison-3.8.1]# cd /usr/local/src
[root@daxia src]# tar zxf boost_1_59_0.tar.gz
[root@daxia src]# mv boost_1_59_0 /usr/local/boost
创建用户和用户组以及mysql存放目录
[root@daxia ~]# groupadd -r mysql
[root@daxia ~]# useradd -r -g mysql -s /bin/false -M mysql
[root@daxia ~]# mkdir -p /usr/local/mysql/data
安装MySQL
[root@daxia src]# cd /usr/local/src
[root@daxia src]# tar zxf mysql-5.7.40.tar.gz
[root@daxia src]# cd mysql-5.7.40/
[root@daxia mysql-5.7.40]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_SYSTEMD=1 -DWITH_BOOST=/usr/local/boost
上述参数说明
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql #[MySQL 安装的根目录] -DMYSQL_DATADIR=/usr/local/mysql /data #[MySQL 数据库文件存放目录] -DSYSCONFDIR=/etc #[MySQL 配置文件所在目录] -DWITH_MYISAM_STORAGE_ENGINE=1 #[添加 MYISAM 引擎支持 ] -DWITH_INNOBASE_STORAGE_ENGINE=1 #[添加InnoDB引擎支持] -DWITH_ARCHIVE_STORAGE_ENGINE=1 #[添加 ARCHIVE 引擎支持 ] -DMYSQL_UNIX_ADDR=/tmp/mysql.sock #[指定 mysql.sock 位置 ] -DWITH_PARTITION_STORAGE_ENGINE=1 #[安装支持数据库分区 ] -DEXTRA_CHARSETS=all #[使MySQL 支持所有的扩展字符] -DDEFAULT_CHARSET=utf8 #[设置 MySQL 的默认字符集为utf8] -DDEFAULT_COLLATION=utf8_general_ci #[设置默认字符集校对规则 ] -DWITH-SYSTEMD=1 #[可以使用systemd控制mysql服务] -DWITH_BOOST=/usr/local/boost #[指向 boost 库所在目录]
编译安装,使用这个方法可以加速安装方式,-j 参数表示根据 CPU 核数指定编译时的线程数,可以加快编译速度。默认为 1 个线程编译。 平常使用的是make && make install
[root@daxia mysql-5.7.40]# make -j $(grep processor /proc/cpuinfo | wc -l) && make install
经过漫长的编译安装过程,如果报错,查看报错信息。如果没有报错,添加path路径
[root@daxia mysql-5.7.40]# vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
[root@daxia mysql-5.7.40]# source /etc/profile
设置权限,"."表示当前目录
[root@daxia mysql-5.7.40]# cd /usr/local/mysql
[root@daxia mysql]# chown -R mysql:mysql .
创建配置文件,如果有原文件就覆盖掉
[root@daxia mysql]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data
pid_file=/usr/local/mysql/data/mysqld.pid
log_error=/usr/local/mysql/data/mysql.log
socket=/tmp/mysql.sock
设置启动,把默认的pid文件指定到/var/run/mysqld/目录。
[root@daxia mysql]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[root@daxia mysql]# vim /usr/lib/systemd/system/mysqld.service #修改下面两行
PIDFile=/usr/local/mysql/data/mysqld.pid #修改pid文件位置
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/data/mysqld.pid $MYSQLD_OPTS #修改pid文件位置
启动MySQL并查看运行状态
[root@daxia mysql]# systemctl daemon-reload
[root@daxia mysql]# systemctl start mysqld
[root@daxia mysql]# systemctl status mysqld
mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2023-06-05 15:00:00 CST; 4s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 114376 ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/data/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 114319 ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 114379 (mysqld)
Tasks: 27
CGroup: /system.slice/mysqld.service
└─114379 /usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/data/mysqld.pid
Jun 05 14:59:56 daxia systemd[1]: Starting MySQL Server...
Jun 05 14:59:56 daxia mysqld_pre_systemd[114319]: mktemp: failed to create file via template ‘/var/lib/mysql-files/install…irectoryJun 05 14:59:56 daxia mysqld_pre_systemd[114319]: chmod: cannot access ‘’: No such file or directory
Jun 05 14:59:56 daxia mysqld_pre_systemd[114319]: /usr/local/mysql/bin/mysqld_pre_systemd: line 43: : No such file or directory
Jun 05 14:59:56 daxia mysqld_pre_systemd[114319]: /usr/local/mysql/bin/mysqld_pre_systemd: line 44: $initfile: ambiguous redirect
Jun 05 15:00:00 daxia systemd[1]: Started MySQL Server.
Hint: Some lines were ellipsized, use -l to show in full.
登录MySQL
[root@daxia mysql]# grep password /usr/local/mysql/data/mysql.log #查看默认密码 密码为u6vh%+jQTrkG
2023-06-05T06:59:57.844601Z 1 [Note] A temporary password is generated for root@localhost: u6vh%+jQTrkG
[root@daxia mysql]# mysqladmin -uroot -p password
Enter password: #输入初始密码
New password: #输入新密码(建议符合密码复杂性要求,否则会提醒)
Confirm new password: #再次输入新密码
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@daxia mysql]# mysql -uroot -p123
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 3
Server version: 5.7.40 Source distribution
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
总结四种安装方式
到这里,我们就安装了四次MySQL服务了,相信你也明白了每种方式的优缺点。rpm和yum安装方式虽然会快,但是不能自定义安装,有些甲方需求无法满足,而二进制安装虽然可以定义一些需求,但是只能简单地一些定义,而编译安装无疑是最复合自定义的要求,但是安装时间较长,需要安装几款编译工具。如果要求不是很多的情况下建议使用二进制安装MySQL,如果是个人练习使用,可以使用rpm或yum方式安装。