Linux中安装mysql数据库

准备工作

Linux系统:CenterOS 7

mysql数据库:https://pan.baidu.com/s/1SO1RQt4DaJ24KUCIIr4pXw 提取码:o3bt

Xhell工具:https://pan.baidu.com/s/1mA_qbo09P8fUeZpQhzO-ng 提取码:op32

上传mysql安装包到Linux服务器

在这里插入图片描述

通过Xftp工具将准备好的mysql安装包上传到Linux服务器中,一般进入Linux系统后默认在root目录下,如果没有linux基础建议先将当前目录切换成root目录

[root@VM-12-16-centos ~]# cd /root
[root@VM-12-16-centos ~]#

mysql压缩包上传完毕后可以通过ll命令查看mysql安装包

[root@VM-12-16-centos ~]# ll
total 875220
-rw-r--r-- 1 root root 896219776 Jun  2 16:27 mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz

开始安装

  1. 使用tar命令解压mysql安装包
[root@VM-12-16-centos ~]# tar -xvf /usr/local/mysql/mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz 
  1. 等待mysql安装包解压完毕后使用ll命令会出现如图信息
[root@VM-12-16-centos ~]# ll
total 875224
drwxr-xr-x 9 root root      4096 Jun  2 16:52 mysql-8.0.25-linux-glibc2.12-x86_64
-rw-r--r-- 1 root root 896219776 Jun  2 16:27 mysql-8.0.25-linux-glibc2.12-x86_64.tar.x
  1. 有Linux基础的可以按照自己的系统管理习惯将mysql安装到自己的管理目录下,这里我们一般会安装到usr/local目录下。现在将mysql的解压文件移动到/usr/local/mysql目录下,在此过程中我们会用mysql替换安装包的长名称。
[root@VM-12-16-centos ~]# mv /root/mysql-8.0.25-linux-glibc2.12-x86_64 /usr/local/mysql
[root@VM-12-16-centos ~]# cd /usr/local/mysql/
[root@VM-12-16-centos mysql]# ll
total 304
drwxr-xr-x  2 7161 31415   4096 Apr 24 02:56 bin
drwxr-xr-x  2 7161 31415   4096 Apr 24 02:56 docs
drwxr-xr-x  3 7161 31415   4096 Apr 24 02:56 include
drwxr-xr-x  6 7161 31415   4096 Apr 24 02:56 lib
-rw-r--r--  1 7161 31415 274942 Apr 23 23:06 LICENSE
drwxr-xr-x  4 7161 31415   4096 Apr 24 02:56 man
-rw-r--r--  1 7161 31415    666 Apr 23 23:06 README
drwxr-xr-x 28 7161 31415   4096 Apr 24 02:56 share
drwxr-xr-x  2 7161 31415   4096 Apr 24 02:56 support-files

PS:root目录下的压缩文件的mysql压缩包可以使用如下命令删除,这一步骤自由选择,对整个安装无影响

[root@VM-12-16-centos mysql]# rm -f /root/mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz 
  1. 创建data目录用于存放mysql配置
    通过ll命令我们可以发现解压的mysql文件夹中是没有data目录的,所以我们需要自己手动创建一个data目录。
[root@VM-12-16-centos mysql]# mkdir data
[root@VM-12-16-centos mysql]# ll
total 308
drwxr-xr-x  2 7161 31415   4096 Apr 24 02:56 bin
drwxr-xr-x  2 root root    4096 Jun  2 17:10 data
drwxr-xr-x  2 7161 31415   4096 Apr 24 02:56 docs
drwxr-xr-x  3 7161 31415   4096 Apr 24 02:56 include
drwxr-xr-x  6 7161 31415   4096 Apr 24 02:56 lib
-rw-r--r--  1 7161 31415 274942 Apr 23 23:06 LICENSE
drwxr-xr-x  4 7161 31415   4096 Apr 24 02:56 man
-rw-r--r--  1 7161 31415    666 Apr 23 23:06 README
drwxr-xr-x 28 7161 31415   4096 Apr 24 02:56 share
drwxr-xr-x  2 7161 31415   4096 Apr 24 02:56 support-files
  1. 创建mysql用户组和用户并将mysql文件夹的拥有者改为指定的用户和用户组
[root@VM-12-16-centos mysql]# groupadd mysql
[root@VM-12-16-centos mysql]# useradd -r -g mysql mysql
[root@VM-12-16-centos mysql]# chown -R mysql:mysql /usr/local/mysql/
[root@VM-12-16-centos mysql]# cd ..
[root@VM-12-16-centos local]# ll
total 52
drwxr-xr-x.  2 root  root  4096 Aug  5  2020 bin
drwxr-xr-x.  2 root  root  4096 Apr 11  2018 etc
drwxr-xr-x.  2 root  root  4096 Apr 11  2018 games
drwxr-xr-x.  2 root  root  4096 Apr 11  2018 include
drwxr-xr-x   3 root  root  4096 Jun  1 18:00 java
drwxr-xr-x.  2 root  root  4096 Apr 11  2018 lib
drwxr-xr-x.  2 root  root  4096 Apr 11  2018 lib64
drwxr-xr-x.  2 root  root  4096 Apr 11  2018 libexec
drwxr-xr-x  10 mysql mysql 4096 Jun  2 17:10 mysql
drwxr-xr-x  14 root  root  4096 May 14 09:04 qcloud
drwxr-xr-x.  2 root  root  4096 Apr 11  2018 sbin
drwxr-xr-x.  5 root  root  4096 Mar  7  2019 share
drwxr-xr-x.  2 root  root  4096 Apr 11  2018 src
srwxrwxrwx   1 root  root     0 May 14 09:08 yd.socket.server
  1. 初始化mysql,并记录mysql初始化密码,如图中root@localhost: psg,ievPu7bA(粗体为密码),每个的激活密码都不一样,安装时根据自己的实际情况保存激活密码。
[root@VM-12-16-centos bin]# cd /usr/local/mysql/bin/
[root@VM-12-16-centos bin]# ./mysqld --user=mysql --initialize --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2021-06-02T09:21:53.893189Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2021-06-02T09:21:53.893325Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.25) initializing of server in progress as process 4406
2021-06-02T09:21:53.907127Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-06-02T09:21:55.594534Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-06-02T09:21:57.525668Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: psg,ievPu7bA
  1. 配置mysql(my.cnf):使用vim命令编辑my.cnf文件,将文件中配置项改为下图所以信息,如果数据库服务器是用于公网环境建议将默认端口3306自行修改。
[root@VM-12-16-centos bin]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
#skip-grant-tables
port=3306
pid-file=/usr/local/mysql/data/mysql.pid
socket=/tmp/mysql.sock
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
default_authentication_plugin=mysql_native_password
  1. 将mysql配置开机自启
[root@VM-12-16-centos bin]# cd /usr/local/mysql/
[root@VM-12-16-centos mysql]# cp support-files/mysql.server /etc/init.d/mysql
[root@VM-12-16-centos mysql]# chmod +x /etc/init.d/mysql
[root@VM-12-16-centos mysql]# chkconfig --add mysql
[root@VM-12-16-centos mysql]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysql          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
opensmd        	0:off	1:off	2:off	3:off	4:off	5:off	6:off
  1. 配置mysql环境变量,使用vim命令进入配置文件,添加下图配置项后保存退出并使用source命令执行配置文件,是配置文件生效。
[root@VM-12-16-centos mysql]# vim /etc/profile
#配置mysql环境变量
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
[root@VM-12-16-centos mysql]# source /etc/profile
  1. 启动mysql
[root@VM-12-16-centos mysql]# systemctl restart mysql
  1. 登陆mysql,使用如下命令后会显示密码输入栏,将前面步骤保存的密码输入到此处后显示如下信息即为登陆成功
[root@VM-12-16-centos mysql]# mysql -uroot -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.25

Copyright (c) 2000, 2021, 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> 

  1. 修改密码
    登陆成功后使用如下命令将数据库密码进行修改,注意,如果是线上数据库服务器建议将密码设置复杂一点
mysql> alter user 'root'@'localhost' identified with mysql_native_password by '123456';

安装完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值