Skip to content

Linux mysql压缩包安装

TIP

安装包在官网下载MySQL :: Download MySQL Community Server (Archived Versions)

本次安装的所有用到的压缩包都在/root目录下

本次安装mysql以5为例

名称版本描述下载地址
mysql5mysql-5.7.30-linux-glibc2.12-x86_64.tar.gzmysql5压缩包下载
mysql8mysql-8.0.32-linux-glibc2.12-x86_64.tar.xzmysql8压缩包下载
目录

解压

进入root目录并解压至/opt/

shell
cd /root
tar -zxvf /opt/software/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /opt/
cd /root
tar -zxvf /opt/software/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /opt/

创建软链接

shell
ln -s mysql-5.7.30-linux-glibc2.12-x86_64 mysql
ln -s mysql-5.7.30-linux-glibc2.12-x86_64 mysql

配置环境变量

shell
vim /etc/profile
vim /etc/profile
sh
export MYSQL_HOME=/opt/mysql
export PATH=$MYSQL_HOME/bin:$PATH
export MYSQL_HOME=/opt/mysql
export PATH=$MYSQL_HOME/bin:$PATH

使变更生效

shell
source /etc/profile
source /etc/profile

创建mysql用户

shell
useradd mysql
useradd mysql

创建目录,并且授权

shell
mkdir -p /opt/mysql/data/
chown mysql:mysql -R /opt/mysql
mkdir -p /opt/mysql/data/
chown mysql:mysql -R /opt/mysql

配置my.cnf文件

shell
vim /etc/my.cnf
vim /etc/my.cnf
cnf
[mysqld]
basedir=/opt/mysql/
datadir=/opt/mysql/data/
socket=/tmp/mysql.sock
# 以mysql用户进行操作
user=mysql
# 临时目录
tmpdir=/tmp/
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/opt/mysql/data/error.log
pid-file=/opt/mysql/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
basedir=/opt/mysql/
datadir=/opt/mysql/data/
socket=/tmp/mysql.sock
# 以mysql用户进行操作
user=mysql
# 临时目录
tmpdir=/tmp/
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/opt/mysql/data/error.log
pid-file=/opt/mysql/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

初始化

这里将密码记住,会有密码出现

shell
mysqld --defaults-file=/etc/my.cnf --basedir=/opt/mysql/ --datadir=/opt/mysql/data/ --user=mysql --initialize
mysqld --defaults-file=/etc/my.cnf --basedir=/opt/mysql/ --datadir=/opt/mysql/data/ --user=mysql --initialize

制作启动文件

进入mysql安装目录

shell
cd /opt/mysql
cd /opt/mysql

拷贝启动文件,并改名为mysqld

shell
cp support-files/mysql.server /etc/init.d/mysqld
cp support-files/mysql.server /etc/init.d/mysqld

设置启动文件的权限

shell
chmod 755 /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld

拷贝文件

shell
cp bin/my_print_defaults /usr/bin
cp bin/my_print_defaults /usr/bin

修改启动脚本,先修改**"/etc/init.d/mysqld"**

shell
vim /etc/init.d/mysqld
vim /etc/init.d/mysqld
sh
# 配置mysql的主目录
basedir=/opt/mysql/

# 配置mysql的数据目录
datadir=/opt/mysql/data/

# 配置mysql端口
port=3306
# 配置mysql的主目录
basedir=/opt/mysql/

# 配置mysql的数据目录
datadir=/opt/mysql/data/

# 配置mysql端口
port=3306

启动服务启动mysql服务

启动服务

shell
service mysqld start
service mysqld start

修改密码

sql
set password for root@localhost=password("123456");
set password for root@localhost=password("123456");

设置mysql远程登陆

sql
use mysql
update user set host = '%' where user='root';
flush privileges;
use mysql
update user set host = '%' where user='root';
flush privileges;

Linux MySQL 安装

本篇文档将介绍如何在 Linux 上安装 MySQL 数据库。我们将按照以下步骤进行安装:

  1. 下载 MySQL 安装包
  2. 解压缩安装包
  3. 配置环境变量
  4. 创建 MySQL 用户
  5. 配置 my.cnf 文件
  6. 初始化 MySQL 数据库
  7. 创建启动文件
  8. 启动 MySQL 服务
  9. 修改密码
  10. 设置 MySQL 远程登录

1. 下载 MySQL 安装包

访问 MySQL 官方网站下载所需的安装包。我们以 MySQL 5.7 为例,下载以下文件:

  • mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

下载地址:https://downloads.mysql.com/archives/community/

2. 解压缩安装包

将下载的安装包解压缩到 /opt/ 目录下。

shell
cd /opt
tar -zxvf /opt/software/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /opt/
cd /opt
tar -zxvf /opt/software/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /opt/

3. 配置环境变量

配置环境变量,以便在终端中使用 mysql 命令。

shell
vim /etc/profile
vim /etc/profile

在文件末尾添加以下内容:

sh
export MYSQL_HOME=/opt/mysql/
export PATH=$MYSQL_HOME/bin:$PATH
export MYSQL_HOME=/opt/mysql/
export PATH=$MYSQL_HOME/bin:$PATH

使环境变量生效:

shell
source /etc/profile
source /etc/profile

4. 创建 MySQL 用户

创建一个名为 mysql 的用户,用于运行 MySQL 服务。

shell
useradd mysql
useradd mysql

5. 配置 my.cnf 文件

编辑 my.cnf 文件,设置相关参数。

shell
vim /etc/my.cnf
vim /etc/my.cnf

在文件中添加以下内容:

cnf
[mysqld]
basedir=/opt/mysql/
datadir=/opt/mysql/data/
socket=/tmp/mysql.sock
user=mysql
tmpdir=/tmp/
symbolic-links=0

[mysqld_safe]
log-error=/opt/mysql/data/error.log
pid-file=/opt/mysql/mysql.pid
[mysqld]
basedir=/opt/mysql/
datadir=/opt/mysql/data/
socket=/tmp/mysql.sock
user=mysql
tmpdir=/tmp/
symbolic-links=0

[mysqld_safe]
log-error=/opt/mysql/data/error.log
pid-file=/opt/mysql/mysql.pid

6. 初始化 MySQL 数据库

使用 mysqld 初始化数据库。

shell
mysqld --defaults-file=/etc/my.cnf --basedir=/opt/mysql/ --datadir=/opt/mysql/data/ --user=mysql --initialize
mysqld --defaults-file=/etc/my.cnf --basedir=/opt/mysql/ --datadir=/opt/mysql/data/ --user=mysql --initialize

7. 创建启动文件

进入 MySQL 安装目录,创建启动文件。

shell
cd /opt/mysql
cd /opt/mysql

拷贝启动文件,并改名为 mysqld

shell
cp support-files/mysql.server /etc/init.d/mysqld
cp support-files/mysql.server /etc/init.d/mysqld

设置启动文件的权限:

shell
chmod 755 /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld

8. 启动服务并设置开机启动

启动 MySQL 服务,并设置开机启动。

shell
service mysqld start
service mysqld start
shell
chkconfig mysqld on
chkconfig mysqld on

9. 修改密码

使用以下命令修改 MySQL 密码:

sql
set password for root@localhost=password("123456");
set password for root@localhost=password("123456");

10. 设置 MySQL 远程登录

使用以下命令允许远程登录:

sql
use mysql;
update user set host = '%' where user='root';
flush privileges;
use mysql;
update user set host = '%' where user='root';
flush privileges;

至此,MySQL 5.7 已经在 Linux 上成功安装并配置完成。你可以使用 mysql 命令进行数据库操作了。