Linux mysql压缩包安装
TIP
安装包在官网下载MySQL :: Download MySQL Community Server (Archived Versions)
本次安装的所有用到的压缩包都在/root目录下
本次安装mysql以5为例
| 名称 | 版本 | 描述 | 下载地址 |
|---|---|---|---|
| mysql | 5 | mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz | mysql5压缩包下载 |
| mysql | 8 | mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz | mysql8压缩包下载 |
目录
解压
进入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 mysqlln -s mysql-5.7.30-linux-glibc2.12-x86_64 mysql配置环境变量
shell
vim /etc/profilevim /etc/profilesh
export MYSQL_HOME=/opt/mysql
export PATH=$MYSQL_HOME/bin:$PATHexport MYSQL_HOME=/opt/mysql
export PATH=$MYSQL_HOME/bin:$PATH使变更生效
shell
source /etc/profilesource /etc/profile创建mysql用户
shell
useradd mysqluseradd mysql创建目录,并且授权
shell
mkdir -p /opt/mysql/data/
chown mysql:mysql -R /opt/mysqlmkdir -p /opt/mysql/data/
chown mysql:mysql -R /opt/mysql配置my.cnf文件
shell
vim /etc/my.cnfvim /etc/my.cnfcnf
[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 --initializemysqld --defaults-file=/etc/my.cnf --basedir=/opt/mysql/ --datadir=/opt/mysql/data/ --user=mysql --initialize制作启动文件
进入mysql安装目录
shell
cd /opt/mysqlcd /opt/mysql拷贝启动文件,并改名为mysqld
shell
cp support-files/mysql.server /etc/init.d/mysqldcp support-files/mysql.server /etc/init.d/mysqld设置启动文件的权限
shell
chmod 755 /etc/init.d/mysqldchmod 755 /etc/init.d/mysqld拷贝文件
shell
cp bin/my_print_defaults /usr/bincp bin/my_print_defaults /usr/bin修改启动脚本,先修改**"/etc/init.d/mysqld"**
shell
vim /etc/init.d/mysqldvim /etc/init.d/mysqldsh
# 配置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 startservice 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 数据库。我们将按照以下步骤进行安装:
- 下载 MySQL 安装包
- 解压缩安装包
- 配置环境变量
- 创建 MySQL 用户
- 配置 my.cnf 文件
- 初始化 MySQL 数据库
- 创建启动文件
- 启动 MySQL 服务
- 修改密码
- 设置 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/profilevim /etc/profile在文件末尾添加以下内容:
sh
export MYSQL_HOME=/opt/mysql/
export PATH=$MYSQL_HOME/bin:$PATHexport MYSQL_HOME=/opt/mysql/
export PATH=$MYSQL_HOME/bin:$PATH使环境变量生效:
shell
source /etc/profilesource /etc/profile4. 创建 MySQL 用户
创建一个名为 mysql 的用户,用于运行 MySQL 服务。
shell
useradd mysqluseradd mysql5. 配置 my.cnf 文件
编辑 my.cnf 文件,设置相关参数。
shell
vim /etc/my.cnfvim /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.pid6. 初始化 MySQL 数据库
使用 mysqld 初始化数据库。
shell
mysqld --defaults-file=/etc/my.cnf --basedir=/opt/mysql/ --datadir=/opt/mysql/data/ --user=mysql --initializemysqld --defaults-file=/etc/my.cnf --basedir=/opt/mysql/ --datadir=/opt/mysql/data/ --user=mysql --initialize7. 创建启动文件
进入 MySQL 安装目录,创建启动文件。
shell
cd /opt/mysqlcd /opt/mysql拷贝启动文件,并改名为 mysqld:
shell
cp support-files/mysql.server /etc/init.d/mysqldcp support-files/mysql.server /etc/init.d/mysqld设置启动文件的权限:
shell
chmod 755 /etc/init.d/mysqldchmod 755 /etc/init.d/mysqld8. 启动服务并设置开机启动
启动 MySQL 服务,并设置开机启动。
shell
service mysqld startservice mysqld startshell
chkconfig mysqld onchkconfig mysqld on9. 修改密码
使用以下命令修改 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 命令进行数据库操作了。