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

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

现代Java服务端开发核心技术

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

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

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

[root@ittimeline Downloads]# yum -y remove 

2.下载MySQL

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

3.解压缩并复制到/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

4.创建数据仓库目录

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

5.新建mysql用户、组和目录

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

6.改变目录所有者

[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

7.配置参数

[root@ittimeline mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql2018-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=457902018-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
  1. 修改系统配置文件
[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 

修改以下内容

basedir=/usr/local/mysqldatadir=/data/mysql

9.启动MySQL

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

10.登录MySQL

[root@ittimeline support-files]# mysql -h127.0.0.1 -uroot -pbash: 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 -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.7.24Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> 

11.MySQL设置

由于MySQL的初始化随机密码不太容易记住,因此这里需要修改生成的随机密码,在登录MySQL后使用如下命令修改,即root用户的密码为root

 mysql> set password=password(‘root‘);

通常情况下都是通过MySQL客户端(例如Navicat)来远程连接MySQL服务端,这里修改root账户的host地址,用来实现远程连接

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; ##切换到mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select host,user from user; ##查看user信息+-----------+---------------+| host | user |+-----------+---------------+| % | root || localhost | mysql.session || localhost | mysql.sys || localhost | root |+-----------+---------------+4 rows in set (0.00 sec)

12.远程登录MySQL

首先使用ifconfig命令查看MySQL所在的centOS服务的IP地址

[root@ittimeline ~]# ifconfigens33: 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 restartRedirecting to /bin/systemctl restart firewalld.service

然后使用Navicat客户端登录MySQL服务,如下图所示

使用Navicat查看远程MySQL服务的user表信息

13 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.pidmysql 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.pidroot 1902 1855 0 21:11 pts/0 00:00:00 grep --color=auto mysql

相关文章