第一讲 MySQL 安装
目录
一、 二进制安装
1.1. 下载二进制安装包
1.2. 解压二进制包
1.3. 准备环境
1.4. 配置并初始化数据库
1.5. 设置环境变量
1.6. 启停 MySQL 服务
二、yum方式安装
2.1. 确认yum源已完成配置
2.2. 安装MySQL服务器软件包
2.3. 启动MySQL服务
2.4. 更改初始密码
三、使用RPM包进行安装
四、源码安装
MySQL的安装方式有多种,以下是其中几种常见的安装方式:
MySQL二进制安装方法相对直接,主要涉及下载二进制包、解压、配置和初始化等步骤。
.tar.gz
为后缀的压缩文件。下载完成后比如我们上传到服务器的 /usr/local 目录。tar
命令解压到/usr/local/
目录下。groupadd mysql
useradd mysql -g mysql –d /home/mysql –s /bin/bash
[root@localhost]# cd mysql
[root@localhost]# chown -R root .
[root@localhost]# chown -R mysql data
[root@localhost]# chgrp -R mysql .
[root@localhost]# cp support-files/my-default.cnf /etc/my.cnf
[root@localhost]# scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql
[root@localhost]# cp support-files/ mysql.server /etc/init.d/mysql
[root@localhost]# echo “PATH=$PATH:/usr/local/mysql/bin”>>/etc/profile
[root@localhost]# source /etc/profile
[root@localhost]# /etc/init.d/mysql start
[root@localhost]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
[root@localhost]# /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
[root@localhost]# /etc/init.d/mysql stop
[root@localhost]# /usr/local/mysql/bin/mysqladmin –uroot –p shutdown
[root@localhost ~]# cat /etc/yum.repos.d/CentOS-Base.repo
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base - mirrors.tuna.tsinghua.edu.cn
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-$releasever
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.tuna.tsinghua.edu.cn
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/RPM-GPG-KEY-CentOS-$releasever
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.tuna.tsinghua.edu.cn
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/RPM-GPG-KEY-CentOS-$releasever
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.tuna.tsinghua.edu.cn
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/RPM-GPG-KEY-CentOS-$releasever
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.tuna.tsinghua.edu.cn
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/contrib/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
gpgcheck=1
enabled=0
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/contrib/$basearch/RPM-GPG-KEY-CentOS-$releasever
yum install mysql-server
上述命令中的 mysql-server 是一个通用的软件包名称,但在某些Linux发行版中,可能需要使用不同的软件包名称。如果您找不到 mysql-server 软件包,请尝试使用 mysql 等替代名称。安装过程中,可能会被提示确认安装和依赖项。请按照提示进行操作,输入y或yes以继续安装。
安装完成后,启动MySQL服务。在终端中输入以下命令:
service mysqld start # 红帽、红旗或者CentOS 7版本以下使用
systemctl start mysqld # 红帽、红旗或者CentOS 7版本以上使用
您还可以将MySQL服务设置为在系统启动时自动启动:
systemctl enable mysqld # 红帽、红旗或者CentOS 7版本以上使用
chkconfig mysqld on # 红帽、红旗或者CentOS 7版本以下使用
安装完成后,MySQL的root用户默认会生成一个随机密码。您可以通过以下命令查看该密码:
grep 'temporary password' /var/log/mysqld.log
请注意,此命令仅适用于首次安装MySQL时生成的临时密码。如果您已经更改过root密码,该命令将不会显示任何内容。
使用获取到的临时密码登录MySQL,然后更改root用户的密码。在终端中输入以下命令:
mysql -u root -p[上一步中获取的临时密码]
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOUR_NEW_PASSWORD';
使用RPM包进行MySQL安装的步骤如下:
# mysql_secure_installation执行步骤:
Securing the MySQL server deployment.
Enter password for user root: # 这里输入当前的root密码
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Change the root password? [Y/n] # 这里输入 Y 或 n
使用RPM包安装MySQL需要具备一定的Linux操作经验,实际工作中我们不常用,或者说基本不用。
MYSQL源码安装的步骤如下:
这个方法最冷门,工作中也不会用这种方法进行安装。