当前位置:Gxlcms > 数据库问题 > 现代Java服务端开发核心技术之CentOS7.5安装MySQL5.7

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

时间:2021-07-01 10:21:17 帮助过:10人阅读

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

MySQL的下载和安装

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

[root@ittimeline Downloads]# yum list installed |grep mysql
[root@ittimeline Downloads]# rpm -qa|grep mysql

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

[root@ittimeline Downloads]# yum -y remove 

下载MySQL

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

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

[root@ittimeline Downloads]# tar -xvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz 

[root@ittimeline Downloads]# mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql

创建数据存储目录

[root@ittimeline Downloads]# mkdir -p /data/mysql

新建mysql用户、组和目录

[root@ittimeline Downloads]# groupadd mysql
[root@ittimeline Downloads]# useradd -r -s /sbin/nologin  -g mysql mysql -d /usr/local/mysql/

改变目录所有者

[root@ittimeline Downloads]# cd /usr/local/mysql/
[root@ittimeline mysql]# chown -R mysql .
[root@ittimeline mysql]# chgrp -R mysql .
[root@ittimeline mysql]# chown -R mysql /data/mysql

MySQL初始化参数配置

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

[root@ittimeline 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 root@localhost: <Xdfhi4Wvy6(

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

[root@ittimeline mysql]# bin/mysql_ssl_rsa_setup  --datadir=/data/mysql

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

[root@ittimeline mysql]# cd /usr/local/mysql/support-files/
[root@ittimeline support-files]# cp mysql.server  /etc/init.d/mysql
[root@ittimeline support-files]# vim /etc/init.d/mysql 

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

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

MySQL的启动和登录

启动MySQL

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

重启MySQL

[root@ittimeline ~]# /etc/init.d/mysql restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS! 

本机客户端登录MySQL

[root@ittimeline support-files]# mysql -h127.0.0.1 -uroot -p
bash: mysql: command not found...
#创建一个软连接
[root@ittimeline support-files]# ln -s /usr/local/mysql/bin/mysql /usr/bin
#再次登录MySQL,输入之前的临时密码
[root@ittimeline 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
[root@ittimeline ~]# 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设置开机启动
[root@ittimeline bin]# chmod 755 /etc/init.d/mysql
[root@ittimeline bin]# chkconfig --add mysql
[root@ittimeline bin]# chkconfig --level 345 mysql on

检测是否开机启动成功

[root@ittimeline bin]# reboot #重启系统
[root@ittimeline ~]# 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地址

[root@ittimeline ~]# 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 命令显示当前防火墙正在运行,如下所示

[root@ittimeline ~]# 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关闭防火墙

[root@ittimeline ~]# systemctl stop firewalld

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

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

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

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

标签:size   bytes   eid   tin   cgroup   height   Fix   creat   disable   

人气教程排行