现代Java服务端开发核心技术之CentOS7.5安装MySQL5.7

Posted ittimeline

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了现代Java服务端开发核心技术之CentOS7.5安装MySQL5.7相关的知识,希望对你有一定的参考价值。

现代Java服务端开发核心技术之CentOS7.5安装mysql5.7

目前主流的MySQL版本为5.6和5.7两个版本,这里介绍MySQL5.7在centOS7.5下的安装、基本配置(修改密码、远程连接、开机启动)及其使用Navicat连接MySQL。

MySQL的下载和安装

首先检查系统中是否已经安装过MySQL,以下提供两种方式

[[email protected] Downloads]# yum list installed |grep mysql
[[email protected] Downloads]# rpm -qa|grep mysql

如果已经安装过,可以使用如下命令删除

[[email protected] Downloads]# yum -y remove 

下载MySQL

[[email protected] Downloads]# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

解压缩并复制到/usr/local/mysql目录下

[[email protected] Downloads]# tar -xvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz 

[[email protected] Downloads]# mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql

创建数据存储目录

[[email protected] Downloads]# mkdir -p /data/mysql

新建mysql用户、组和目录

[[email protected] Downloads]# groupadd mysql
[[email protected] Downloads]# useradd -r -s /sbin/nologin  -g mysql mysql -d /usr/local/mysql/

改变目录所有者

[[email protected] Downloads]# cd /usr/local/mysql/
[[email protected] mysql]# chown -R mysql .
[[email protected] mysql]# chgrp -R mysql .
[[email protected] mysql]# chown -R mysql /data/mysql

MySQL初始化参数配置

指定mysql服务端的用户以及安装目录和数据存放的目录以及生成root账号密码

[[email protected] mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
2018-10-27T03:09:02.164480Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-27T03:09:02.764905Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-10-27T03:09:02.852039Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-10-27T03:09:02.923109Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: a8263578-d995-11e8-a863-000c29be37a9.
2018-10-27T03:09:02.924734Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2018-10-27T03:09:02.925516Z 1 [Note] A temporary password is generated for [email protected]: <Xdfhi4Wvy6(

此处需要注意生成的临时密码 ,如上问结尾处的<Xdfhi4Wvy6(

[[email protected] mysql]# bin/mysql_ssl_rsa_setup  --datadir=/data/mysql

修改系统配置文件,复制mysql.server到/etc/init.d/mysql

[[email protected] mysql]# cd /usr/local/mysql/support-files/
[[email protected] support-files]# cp mysql.server  /etc/init.d/mysql
[[email protected] support-files]# vim /etc/init.d/mysql 

然后配置mysql的安装目录和数据存储目录,修改以下内容

basedir=/usr/local/mysql
datadir=/data/mysql

MySQL的启动和登录

启动MySQL

[[email protected] support-files]# /etc/init.d/mysql start
Starting MySQL.Logging to ‘/data/mysql/ittimeline.net.err‘.
 SUCCESS! 

重启MySQL

[[email protected] ~]# /etc/init.d/mysql restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS! 

本机客户端登录MySQL

[[email protected] support-files]# mysql -h127.0.0.1 -uroot -p
bash: mysql: command not found...
#创建一个软连接
[[email protected] support-files]# ln -s /usr/local/mysql/bin/mysql /usr/bin
#再次登录MySQL,输入之前的临时密码
[[email protected] support-files]# mysql -h127.0.0.1 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.24

Copyright (c) 2000, 2018, 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的基本配置

  1. 修改随机生成的root密码
    由于MySQL的初始化随机密码不太容易记住,因此这里需要修改生成的随机密码,在使用root登录MySQL后使用如下命令修改,即root用户的密码为root
[[email protected] ~]# mysql -uroot -p -h127.0.0.1
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 21
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> set password=password(‘root‘);
Query OK, 0 rows affected, 1 warning (0.00 sec)

  1. 配置远程登录连接

通过授权root账号,设置root的host为%,表示任意的客户端都可以通过root账户登录到MySQL服务器。

mysql> grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘root‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

查看mysql数据库的user表的host信息,当root的host值为%则可以实现远程链接


mysql> use mysql; ##切换到mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user from user; ##查看user信息
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
4 rows in set (0.00 sec)
  1. MySQL设置开机启动
[[email protected] bin]# chmod 755 /etc/init.d/mysql
[[email protected] bin]# chkconfig --add mysql
[[email protected] bin]# chkconfig --level 345 mysql on

检测是否开机启动成功

[[email protected] bin]# reboot #重启系统
[[email protected] ~]# ps -ef|grep mysql #使用ps命令查看MySQL进程信息
root       1052      1  0 21:10 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/ittimeline.net.pid
mysql      1242   1052  1 21:10 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=ittimeline.net.err --pid-file=/data/mysql/ittimeline.net.pid
root       1902   1855  0 21:11 pts/0    00:00:00 grep --color=auto mysql

MySQL的远程连接

通常情况下都是通过MySQL客户端(例如Navicat)来远程连接MySQL服务端,首先使用ifconfig命令查看MySQL所在的centOS服务的IP地址

[[email protected] ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.105  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::20c:29ff:febe:37a9  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:be:37:a9  txqueuelen 1000  (Ethernet)
        RX packets 1412887  bytes 2058226957 (1.9 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 276749  bytes 22287862 (21.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

然后检测防火墙的状态
systemctrl status firewalld 命令显示当前防火墙正在运行,如下所示

[[email protected] ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2018-10-26 20:42:21 PDT; 24s ago
     Docs: man:firewalld(1)
 Main PID: 9444 (firewalld)
    Tasks: 2
   CGroup: /system.slice/firewalld.service
           └─9444 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

此时我们需要使用 systemctl stop firewalld关闭防火墙

[[email protected] ~]# systemctl stop firewalld

或者使用如下命令让防火墙放行3306端口

[[email protected] ~]# firewall-cmd  --zone=public --add-port=3306/tcp --permanent  
success
[[email protected] ~]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service

然后使用Navicat客户端登录MySQL服务,如下图所示
技术分享图片




以上是关于现代Java服务端开发核心技术之CentOS7.5安装MySQL5.7的主要内容,如果未能解决你的问题,请参考以下文章

现代Java服务端开发核心技术之Java基础项目搭建

现代Java服务端开发核心技术之Java基础项目搭建

现代Java服务端开发核心技术之开发工具箱

现代Java服务端开发核心技术之开发工具箱

现代Java服务端开发核心技术之开发工具箱

现代Java服务端开发核心技术之开发工具箱