centos7环境 安装jdk mysql

centos7 安装 jdk8

1.下载jdk8

官网: oracle jdk8 官网 下载页面

在这里插入图片描述

需注册或登录oracle账号才可下载

2.上传至服务器

  • 命令上传
# 将文件上传至/usr/local/soft 文件夹下 若文件夹不存在需提前mkdir

# scp .\jdk-8u271-linux-x64.tar.gz root@192.168.0.106:/usr/local/soft/jdk-8u271-linux-x64.tar.gz
  • 客户端上传

使用WinSCP \ Xftp 都可以

3.解压 安装

  • 解压
#进入centos7 命令行 解压文件到/usr/local/env/目录下
[root@192 soft]# ls
jdk-8u271-linux-x64.tar.gz  redis-6.0.9  redis-6.0.9.tar.gz
[root@192 soft]# mkdir -p /usr/local/env/
[root@192 soft]# tar -zxf jdk-8u271-linux-x64.tar.gz -C /usr/local/env/
  • 配置环境变量

    • 为所有用户安装
    #将
    export JAVA_HOME=/usr/local/env/jdk1.8.0_271
    export PATH=$PATH:$JAVA_HOME/bin
    #追加到/etc/profile 文件中 可使用下一条命令追加 也可使用vim /etc/profile 追加	
    
    [root@192 env]# echo -e 'export JAVA_HOME=/usr/local/env/jdk1.8.0_271 \nexport PATH=$PATH:$JAVA_HOME/bin' >> /etc/profile 
    
    # 输出一下JAVA_HOME 未输出
    [root@192 env]# echo $JAVA_HOME
    
    [root@192 env]# source /etc/profile  
    #刷新一下 /etc/profile  文件
    
    # 再试输出 $JAVA_HOME 成功
    [root@192 env]# echo $JAVA_HOME
    /usr/local/env/jdk1.8.0_271
    [root@192 env]# echo $PATH
    /opt/rh/devtoolset-9/root/usr/bin:/opt/rh/devtoolset-9/root/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/env/jdk1.8.0_271/bin
    
    # 查看jdk版本 打印一下说明安装成功
    [root@192 env]# java -version 
    java version "1.8.0_271"
    Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
    Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)
    
    • 为当前用户安装
    #将
    export JAVA_HOME=/usr/local/env/jdk1.8.0_271
    export PATH=$PATH:$JAVA_HOME/bin
    #追加到 ~/.bashrc 文件中 可使用下一条命令追加 也可使用vim ~/.bashrc 追加	
    
    [qhg@192 ~]$ echo -e '\nexport JAVA_HOME=/usr/local/env/jdk1.8.0_271\nexport PATH=$PATH:$JAVA_HOME/bin'>> ~/.bashrc 
    
    #刷新 ~/.bashrc 文件
    [qhg@192 ~]$ source ~/.bashrc 
    
    [qhg@192 ~]$ echo $JAVA_HOME
    /usr/local/env/jdk1.8.0_271
    
    # 查看jdk版本 打印以下 说明成功
    [qhg@192 ~]$ java -version
    java version "1.8.0_271"
    Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
    Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)
    
    

centos7 安装Mysql 5.7

1.下载Mysql5.7.31

Mysql官网下载

在这里插入图片描述

2.上传至服务器

  • 命令上传
# scp .\mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz root@192.168.0.106:/usr/local/soft/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
  • 客户端上传

使用WinSCP \ Xftp 都可以

3.解压 安装

  • 解压
# 解压文件到 /usr/local/env/ 目录下
[root@192 soft]# tar -zxf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/env/
# 名字太长 修改短点
[root@192 env]# mv mysql-5.7.31-linux-glibc2.12-x86_64/ mysql-5.7.31
  • 创建mysql 用户及mysql用户组 并更改mysql-5.7.31 所属组和用户

    • 创建mysql 用户组
    [root@192 env]# groupadd mysql
    
    • 创建mysql 用户
    [root@192 env]# useradd -g mysql mysql 
    
    • 设置mysql用户密码
    [root@192 env]# passwd mysql
    
    • 更改mysql-5.7.31 所属组和用户
    [root@192 env]# chown -R mysql  mysql-5.7.31/
    [root@192 env]# chgrp -R mysql  mysql-5.7.31/
    
  • 创建 data 文件夹 以及编辑 mysql 配置文件

    • 创建data 文件夹
    [root@192 mysql-5.7.31]# mkdir -p /data/mysql
    
    • 编辑my.cnf 配置文件

      # 删除 /etc/my.cnf
      [root@192 mysql-5.7.31]# rm /etc/my.cnf
      # 新建 my.cnf
      [root@192 mysql-5.7.31]# touch /etc/my.cnf
      
      # 编辑 /etc/my.cnf
      [root@192 mysql-5.7.31]# vim /etc/my.cnf
      # 将以下内容复制到my.cnf中 esc -> : > wq 保存退出
      --------------------------------------------------
      [mysql]
      # 设置mysql客户端默认字符集
      default-character-set=utf8
      
      [mysqld]
      
      skip-name-resolve
      
      #设置3306端口
      port = 3306
      
      # 设置mysql的安装目录
      basedir=/usr/local/env/mysql-5.7.31
      
      # 设置mysql数据库的数据的存放目录
      datadir=/data/mysql
      
      # 允许最大连接数
      max_connections=200
      
      # 服务端使用的字符集默认为8比特编码的latin1字符集
      character-set-server=utf8mb4
      
      # 创建新表时将使用的默认存储引擎
      default-storage-engine=INNODB
      lower_case_table_names=1
      max_allowed_packet=16M
      symbolic-links=0
      explicit_defaults_for_timestamp=true
      log-error=/data/mysql/mysql.err
      pid-file=/data/mysql/mysql.pid
      
      [mysqld_safe]
      #log-error=/var/log/mariadb/mariadb.log
      log-error=/data/mysql/mariadb.log
      #pid-file=/var/run/mariadb/mariadb.pid
      pid-file=/data/mysql/mariadb.pid
      
      
  • 安装初始化 并 查看密码

    • 初始化
    [root@192 mysql-5.7.31]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/env/mysql-5.7.31/ --datadir=/data/mysql
    
    • 查看初始密码
    [root@192 mysql-5.7.31]# cat /data/mysql/mysql.err | grep password
    2020-12-02T13:51:40.026586Z 1 [Note] A temporary password is generated for root@localhost: R<juLqRP0y#e
    # 我这边的初始密码是 R<juLqRP0y#e
    
  • 添加mysql服务并启动

    • 添加mysql服务
    [root@192 mysql-5.7.31]# cp ./support-files/mysql.server /etc/init.d/mysqld
    
    • 启动
    [root@192 mysql-5.7.31]# service mysqld start
    Starting MySQL.Logging to '/data/mysql/mariadb.log'.
     SUCCESS! 
    
  • 访问mysql服务 并 修改初始密码

    • 访问mysql服务
    [root@192 mysql-5.7.31]# ./bin/mysql -u root -p
    #此处输入你的初始密码 示例:R<juLqRP0y#e
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.31
    
    Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
    
    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命令行
    mysql> 
    
    • 修改密码(任选一种)

      • 第一种方式
      # 重置当前账户密码为root (设置为你想要的密码);
      mysql> set password = password('root');
      Query OK, 0 rows affected, 1 warning (0.00 sec)
      # 设置密码永不过期
      mysql> alter user 'root'@'localhost' password expire never;
      Query OK, 0 rows affected (0.01 sec)
      # 刷新
      mysql> flush privileges;
      Query OK, 0 rows affected (0.01 sec)
      #退出
      mysql> exit;
      Bye
      #使用新密码重试
      [root@192 mysql-5.7.31]# ./bin/mysql -u root -p
      #输入你的新密码:
      Enter password: 
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 10
      Server version: 5.7.31 MySQL Community Server (GPL)
      
      Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
      
      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> exit
      Bye
      
      
      • 第二种方式
      # 重置root账户密码为root (设置为你想要的密码);
      mysql> set password for root@localhost=password('123456');
      Query OK, 0 rows affected, 1 warning (0.00 sec)
      # 刷新
      mysql> flush privileges;
      Query OK, 0 rows affected (0.00 sec)
      #退出
      mysql> exit;
      Bye
      #使用新密码重试
      [root@192 mysql-5.7.31]# ./bin/mysql -u root -p
      #输入你的新密码:
      Enter password: 
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 10
      Server version: 5.7.31 MySQL Community Server (GPL)
      
      Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
      
      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> exit
      Bye
      
  • 现在访问mysql服务需要相对路径(或全路径)去访问

[root@192 mysql-5.7.31]# ./bin/mysql -u root -p
Enter password: 
[root@192 /]# /usr/local/env/mysql-5.7.31/bin/mysql -uroot -p
Enter password: 
#直接使用mysql -u root -p 会报错 解决办法就是将 mysql添加至环境变量
[root@192 /]# mysql -u root -p
-bash: mysql: command not found
  • 将/usr/local/env/mysql-5.7.31/bin 添加至环境变量

    • 方式一
    #将
    export MYSQL_HOME=/usr/local/env/mysql-5.7.31
    export PATH=$PATH:$MYSQL_HOME/bin
    # 追加到/etc/profile 文件后
    
    [root@192 /]# echo -e '\nexport MYSQL_HOME=/usr/local/env/mysql-5.7.31\nexport PATH=$PATH:$MYSQL_HOME/bin'>>/etc/profile
    # 刷新
    [root@192 /]# source /etc/profile
    # 输出一下确认是否正确
    [root@192 /]# echo $MYSQL_HOME
    /usr/local/env/mysql-5.7.31
    [root@192 /]# echo $PATH
    /opt/rh/devtoolset-9/root/usr/bin:/opt/rh/devtoolset-9/root/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/env/mysql-5.7.31/bin
    # 在任意路径试着访问 不报错 ok
    [root@192 /]# mysql -u root -p
    Enter password: 
    [root@192 opt]# mysql -u root -p
    Enter password: 
    
    • 方式二
    [root@192 /]# vim /etc/profile 
    #将
    export MYSQL_HOME=/usr/local/env/mysql-5.7.31
    export PATH=$PATH:$MYSQL_HOME/bin
    # 追加到/etc/profile 文件后 esc > :wq 保存退出 
    
    # 刷新
    [root@192 /]# source /etc/profile
    
    # 在任意路径试着访问 不报错 ok
    [root@192 /]# mysql -u root -p
    Enter password: 
    yum install net-tools
    
     mysql -u root -p
    Enter password: 
    

完成安装!!!

4.开启远程访问

  • 前提 需要开放centos mysql 3306 端口 若不知道mysql运行在那个端口可先查看

    • 查看mysql端口
    #列出所有服务端口
    [root@192 opt]# netstat -lntup
    #命令没找到就安装
    -bash: netstat: command not found
    #net-tools安装 根据netstat -lntup 命令是否报错在执行 执行netstat报错在安装
    [root@192 /]# yum install net-tools -y
    [root@192 opt]# netstat -lntup
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1189/sshd           
    tcp6       0      0 :::3306                 :::*                    LISTEN      3289/mysqld         
    tcp6       0      0 :::22                   :::*                    LISTEN      1189/sshd           
    udp        0      0 0.0.0.0:68              0.0.0.0:*                           997/dhclient 
    #出现下面表格结果
    
    ProtoRecv-QSend-QLocalAddressForeign AddressStatePID/Program name
    tcp000.0.0.0:220.0.0.0:*LISTEN1189/sshd
    tcp600:::3306:: : *LISTEN3289/mysqld
    tcp600:::22:: : *LISTEN1189/sshd
    udp000.0.0.0:680.0.0.0:*997/dhclient
    #不想展示过多可通过此命令直接查找mysql端口
    [root@192 opt]# netstat -lntup | grep mysql
    tcp6       0      0 :::3306                 :::*                    LISTEN      3289/mysqld 
    
    • 开放端口

      • 将3306端口添加至防火墙过滤规则
      #将3306端口添加至防火墙
      [root@192 ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
      success
      # 重新加载
      [root@192 ~]# firewall-cmd --reload
      success
      # 查看以开放端口列表
      [root@192 ~]# firewall-cmd --list-ports
      80/tcp 3306/tcp
      
      • 直接关闭防火墙
      # 查看防火墙状态
      [root@192 ~]# firewall-cmd --state
      running
      # 关闭防火墙
      [root@192 ~]# systemctl stop firewalld.service
      # 再次查看状态
      [root@192 ~]# firewall-cmd --state
      not running
      
  • 开启远程访问

    • 进入mysql命令交互行开启
    # 对root用户 接受任何ip(%=任意ip,也可写成指定ip) 使用123456作为密码来访问
    mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    # 刷新
    mysql> FLUSH PRIVILEGES; 
    Query OK, 0 rows affected (0.00 sec)
    
    • 远程访问测试

      • 客户端访问 navicat 或 sqlyog
      • windows环境访问
      C:\Users\qiaohuiguo\Desktop
      # mysql -uroot -h 192.168.0.106 -p
      Enter password: ******
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 33
      Server version: 5.7.31 MySQL Community Server (GPL)
      
      Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
      
      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>
      
  • mysql服务开机自启

# 将mysql 服务添加至服务列表
[root@192 opt]# chkconfig --add mysqld
# 查看列表 MySQL列 2345 都为off
[root@192 opt]# 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]'.

mysqld         	0:off	1:off	2:off	3:off	4:off	5:off	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

# 设置 mysqld on
[root@192 opt]# chkconfig mysqld on
# 再次查看列表 mysqld 列 2345 都为on
[root@192 opt]# 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]'.

mysqld         	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

Linux OS 将操作 环境分为以下7个等级,即

0:关机
1:单用户模式(单用户、无网络)
2:无网络支持的多用户模式(多用户、无网络)
3:有网络支持的多用户模式(多用户、有网络)
4:保留,未使用
5:有网络支持有X-Window支持的多用户模式(多用户、有网络、X-Window界面)
6:重新引导系统,即重启

Linux中有多种运行级,常见的就是多用户的2,3,4,5 ,很 多人知道5是运行X-Windows的级别,而0就是关机了。运行级的改变可以通过init命令来切换。例如,假 设你要维护系统进入单用户状态,那 么,可以使用init1来切换。在Linux的运行级的切换过程中,系统会自动寻找对应运行级的目录/etc/rc[0-6].d下的K和S开头的文件, 按后面的数字顺序,执行这些脚本。对这些脚本的维护,是很繁琐的一件事情,Linux提供了chkconfig命令用来更新和查询不同运行级上的系统服 务。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值