安装jdk、安装tomcat、配置tomcat、tomcat日志、主从数据库、mysql常用命令

tech2022-07-10  193

一、搭建配置Tomcat服务

任务1 Tomcat介绍

1. 简介

Tomcat是Apache软件基金会( Apache Software Foundation )的Jakarta项目中的一个核心项目,由Apache、Sun和其他一些公司及个人共同开发而成。受Java爱好者的喜爱,并得到了部分软件开发商的认可,成为目前比较流行的Web应用服务器。 Tomcat服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。目前有很多网站是用Java编写的,所以解析Java程序就必须有相关的软件来完成,Tomcat就是其中之一。 Java程序写的网站用Tomcat+JDK来运行。 Tomcat是一个中间件,真正起作用的,解析Java脚本的是JDK。 JDK(Java Development Kit)是整个Java的核心,它包含了Java运行环境和一堆Java相关的工具以及Java基础库。最主流的JDK为Sun公司发布的JDK,除此之外,其实IBM公司也有发布JDK,CentOS上也可以用yum安装OpenJDK。 2. 环境准备 规划节点,本次实验为单节点部署:

节点IP主机名节点192.168.200.20tomcatTomcat

使用VMWare Workstation软件安装CentOS 7.2操作系统,镜像使用提供的CentOS-7-x86_64-DVD-1511.iso。 关闭防火墙并设置开机不自启,配置SElinux规则。

[root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# systemctl disable firewalld.service Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service. [root@localhost ~]# setenforce 0 修改主机名 使用hostnamectl命令修改主机名,命令如下: [root@localhost ~]# hostnamectl set-hostname tomcat // 修改完后Ctrl+D退出后重新连接 [root@tomcat ~]#

任务2 安装JDK

JDK的下载 将提供的安装包JDK安装包和Tomcat安装包。通过CRT上传JDK到/usr/local/src/目录下。JDK安装 部署Tomcat环境需要JDK软件环境,我们进入到/usr/local/src/目录下,解压刚刚上传的jdk将解压后的文件移动到/usr/local/目录下改名为jdk1.8。然后使用ls命令查看下/usr/local/目录下是否有jdk1.8目录。 命令如下: [root@tomcat ~]# cd /usr/local/src/ [root@tomcat src]# tar zxf jdk-8u211-linux-x64.tar.gz [root@tomcat src]# mkdir /usr/local/jdk1.8 [root@tomcat src]# mv jdk1.8.0_211/* /usr/local/jdk1.8 [root@tomcat src]# ls /usr/local/ bin etc games include jdk1.8 lib lib64 libexec sbin share src 修改环境变量 设置环境变量,编辑/etc/profile文件,并使其立即生效。 [root@tomcat src]# vi /etc/profile //将以下内容添加到文件底部 ...... JAVA_HOME=/usr/local/jdk1.8/ JAVA_BIN=/usr/local/jdk1.8/bin JRE_HOME=/usr/local/jdk1.8/jre PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charsets.jar [root@tomcat src]# source /etc/profile 检查JDK安装情况 配置完成环境变量后,使用java -version命令检查是否安装成功,如果显示结果带有java version "1.8.0_211"字样和我们解压的jdk-8u211-linux-x64.tar.gz包的版本相对应,则证明安装成功。命令如下: [root@tomcat src]# java -version java version "1.8.0_211" Java(TM) SE Runtime Environment (build 1.8.0_211-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode) [root@tomcat src]#

这里有可能出现的不是上面的这种,而是如下所示:

[root@tomcat src]# java -version openjdk version "1.8.0_242" OpenJDK Runtime Environment (build 1.8.0_242-b08) OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)

如果发现不是自己安装的JDK,这是因为系统自带OpenJDK或者以前安装过OpenJDK。使用witch命令,查看现在Java的所在目录:

[root@tomcat src]# which java /usr/bin/java

如果结果为/usr/bin/java则说明这是系统自带的OpenJDK。这时,我们为了实验的一致性,把原来的Java目录重命名为java_bak,并使用source命令再次使环境变量生效,然后使用java -verison命令查看是否有java version "1.8.0_211"字样,命令如下: [root@tomcat src]# mv /usr/bin/java /usr/bin/java_bak

[root@tomcat src]# source /etc/profile [root@tomcat src]# java -version java version "1.8.0_211" Java(TM) SE Runtime Environment (build 1.8.0_211-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode) [root@tomcat src]#

任务3 安装Tomcat

安装Tomcat 解压Tomcat软件包,将解压后的文件移动到/usr/local/tomcat目录下,并命名为tomcat,命令如下: [root@tomcat ~]# cd /usr/local/src/ [root@tomcat src]# tar zxf apache-tomcat-9.0.21.tar.gz [root@tomcat src]# mkdir /usr/local/tomcat [root@tomcat src]# mv apache-tomcat-9.0.21/* /usr/local/tomcat 启动Tomcat 因解压的包是二进制包,不用我们去编译,使用/usr/local/tomcat/bin/startup.sh这个命令启动Tomcat,命令如下: [root@tomcat src]# /usr/local/tomcat/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@tomcat src]# 测试 用netstat命令来监听java相关服务端口,查看是否有以下端口存在(8009、8080、8005),如果存在则证明Tomcat服务启动成功。命令如下: [root@tomcat src]# netstat -lnpt | grep java tcp6 0 0 :::8009 :::* LISTEN 49228/java tcp6 0 0 :::8080 :::* LISTEN 49228/java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 49228/java [root@tomcat src]#

三个端口8009、8005和8080的意义: 8080为提供Web服务的端口;  8005为管理端口;  8009端口为第三方服务调用的端口,比如httpd和Tomcat结合时会用到。 然后打开浏览器,在地址栏中输入http://IP:8080/(这里的IP为你虚拟机的IP地址,此处IP为192.168.174.155),可以看到Tomcat的默认页面。如图所示:

任务4 配置Tomcat监听80端口

修改配置文件 Tomcat 监听的是8080端口,如果想要直接访问,不加8080端口(默认输入一个IP或者域名,访问80端口时是可以省略IP后端口号的)就需要配置Tomcat监听80端口,Tomcat是支持端口自定义的。 编辑Tomcat配置文件server.xml,命令如下: [root@tomcat ~]# vi /usr/local/tomcat/conf/server.xml //直接搜索8080找到如下内容 ...... <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> //将这里的8080直接改成80 ......

修改完配置文件,接下来就是重启Tomcat服务,Tomcat服务是不支持restart的方式重启服务的,所以要想重启服务必须先关闭服务,使用命令/usr/local/tomcat/bin/shutdown.sh,然后再启动服务,使用命令/usr/local/tomcat/bin/startup.sh。

[root@tomcat ~]# /usr/local/tomcat/bin/shutdown.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [root@tomcat ~]# [root@tomcat ~]# /usr/local/tomcat/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. 测试 这时,我们再次使用netstat命令监听Java相关服务端口,命令结果如下: [root@tomcat ~]# netstat -plnt | grep java tcp6 0 0 :::8009 :::* LISTEN 47873/java tcp6 0 0 :::80 :::* LISTEN 47873/java tcp6 0 0 127.0.0.1:8005 :::* LISTEN 47873/java

可以看到这次三个服务端口都存在,证明Tomcat服务启动成功。 3. 浏览器访问 配置文件修改完成,并Tomcat服务启动成功后,打开浏览器在地址栏中输入http://192. 168.174.155/(注意你的IP),这时又会看见Tomcat默认页面。如图所示。

任务5 配置Tomcat虚拟主机

查看默认虚拟主机 Tomcat和Apache、Nginx一样,都支持虚拟主机配置。每个虚拟主机配置文件就是一台服务器,支持一个IP下可以访问多个域名,每个域名代表一个网站。所以一台服务器上可以配置多个网站。 编辑Tomcat配置文件,直接在配置文件内搜索Host,找到如下内容: [root@tomcat ~]# vi /usr/local/tomcat/conf/server.xml ...... <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" /> </Host> ......

其中和之间的配置为虚拟主机配置部分:  name定义域名;  appBase定义应用的目录;  unpackWARs为是否自动解压war包;  autoDeploy 如果此项设置为true,表示Tomcat服务处于运行状态,能够检测appbase下的文件,如果有新的Web应用加入进来,会自动发布这个Web应用。 Java的应用通常是一个JAR的压缩包,你只需要将JAR的压缩包放到appBase目录下面即可。刚刚我访问的Tomcat默认页其实就是在appBase目录下面,不过是在它子目录ROOT里。 2. 新增虚拟主机 新增虚拟主机,编辑server.xml在下面增加以下内容:

[root@tomcat ~]# vi /usr/local/tomcat/conf/server.xml ...... <Host name="www.123.cn" appBase="" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context path="" docBase="/data/wwwroot/123.cn/" debug="0" reloadable="true" crossContext="true"/> </Host>

docBase这个参数用来定义网站的文件存放路径。如果不定义,默认是在appBase/ROOT下面。定义了docBase就以该目录为主了,其中appBase和docBase可以一样。在这一步操作过程中很多人会遇到过访问404的问题,其实就是docBase没有定义对。 appBase为应用存放目录(实际上是一个相对路径,相对于 /usr/local/tomcat/ 路径),通常是需要把war包直接放到该目录下面,它会自动解压成一个程序目录。 搭建了一个Tomcat,想要使用Tomcat去跑一个网站。首先应用不能是一个传统所谓的目录(Apache、Nginx访问网站,首先需要指定一个目录,目录里存放着PHP文件或者是Html的文件,然后去访问),Tomcat需要提供一个war的包,就是一个压缩包,这个压缩包里面包含着运行这个网站的一些文件,包括配置,js代码,数据库相关的等等,都需要打包成war这种文件,而这个文件需要放置到 webapps 里面。 3. appBase部署Java应用 下面我们通过部署个Java的应用来体会appBase和docBase目录的区别。 为了方便测试,下载一个zrlog(Java写的blog站点应用,轻量),zrlog实际就是一个war包。 (下载地址:http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war) 先暂时将zrlog的war包下载到/usr/local/src目录下,命令如下:

[root@tomcat ~]# cd /usr/local/src/ [root@tomcat src]# wget http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war ... [root@tomcat src]# ls |grep zrlog zrlog-1.7.1-baaecb9-release.war

appBase支持自动解压,所以直接将war包拷贝到/usr/local/tomcat/webapps/目录下:

[root@tomcat src]# cp zrlog-1.7.1-baaecb9-release.war /usr/local/tomcat/webapps/ [root@tomcat src]# ls /usr/local/tomcat/webapps/ docs host-manager ROOT zrlog-1.7.1-baaecb9-release.war examples manager zrlog-1.7.1-baaecb9-release // 将war包拷贝到/usr/local/tomcat/webapps/目录下之后,包会自动解压。(前提是tomcat正常启动。

重命名war包的文件名,命令如下:

[root@tomcat src]# cd /usr/local/tomcat/webapps/ [root@tomcat webapps]# mv zrlog-1.7.1-baaecb9-release zrlog [root@tomcat webapps]# ls docs examples host-manager manager ROOT zrlog zrlog-1.7.1-baaecb9-release.war [root@tomcat webapps]# ls docs host-manager ROOT zrlog-1.7.1-baaecb9-release examples manager zrlog zrlog-1.7.1-baaecb9-release.war // 一但重命名或删除war包解压后的包的文件时,war包就会在解压出一个文件夹。

用浏览器访问http://192. 168.174.155/zrlog(注意你的IP),如图所示:

出现安装向导,这是一个配置数据库的过程。 首先安装数据库服务,登入数据库,在数据库中创建一个zrlog数据库和zrlog用户。命令如下:

[root@tomcat webapps]# yum -y install mariadb mariadb-server [root@tomcat webapps]# systemctl start mariadb [root@tomcat webapps]# mysql_secure_installation //初始化数据库 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.39 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> create database zrlog; //创建zrlog数据库 Query OK, 1 row affected (0.00 sec) mysql> grant all on zrlog.* to 'zrlog'@127.0.0.1 identified by '000000'; Query OK, 0 rows affected (0.05 sec) //创建zrlog用户 mysql>exit Byebye //退出数据库

检查创建用户是否可以登陆数据库,使用zrlog用户登录,命令如下:

[root@tomcat webapps]# mysql -uzrlog -h127.0.0.1 -p000000 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.39 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> show databases; //检查已有数据库 +--------------------+ | Database | +--------------------+ | information_schema | | test | | zrlog | +--------------------+ 3 rows in set (0.00 sec) mysql> exit Byebye

检查完成,zrlog用户登录成功。使用 zrlog用户信息填写刚才在浏览器中打开的网页,Email填写自己的邮箱,本次是实验,填写内容为自定义邮箱(tomcat@163.com),单击“下一步”按钮,如图所示:

设置管理员账号(admin)和管理员密码(123456),网站标题和子标题按需填写,本次自定义内容(网站标题:“测试”,网站子标题:“linux”),如图所示:

填写完成后,单击“下一步”按钮,可以看到安装完成的界面。如图所示:

单击“点击查看”按钮,我们就可以进入搭建好的zrlog页面了。如图所示:

我们也可以进入管理页面,写一些文章,单击上图主菜单栏中的“管理”按钮,跳转如图所示:

输入安装向导里已经设置好的账户名和密码(admin:123456),单击“登录”按钮,登录成功页面,如图所示:

单击“文章撰写”栏目,写上自己想写的内容,然后保存,如图所示:

然后回到主页面,你就可以看到刚才你写的内容。

任务5 Tomcat日志

查看日志 Tomcat在应用过程中,难免会出现错误,如何去查看这些错误,这就需要查看Tomcat的日志。Tomcat日志存放在/usr/local/tomcat/logs/目录下。命令如下: [root@tomcat ~]# ls /usr/local/tomcat/logs/ catalina.2020-03-29.log localhost.2020-03-29.log catalina.out localhost_access_log.2020-03-29.txt host-manager.2020-03-29.log manager.2020-03-29.log [root@tomcat ~]#

 catalina开头的日志为Tomcat的综合日志,它记录Tomcat服务相关信息,也会记录错误日志。  catalina.2020-xx-xx.log和catalina.out内容相同,前者会每天生成一个新的日志。  host-manager和manager为管理相关的日志,其中host-manager为虚拟主机的管理日志。  localhost和localhost-access为虚拟主机相关日志,其中带access字样的日志为访问日志,不带access字样的为默认虚拟主机的错误日志。 2. 配置生成日志 由于日志默认不会生成,需要在server.xml中配置一下。所以接下来配置新增虚拟主机的访问日志,具体方法是在对应虚拟主机的里面加入下面的配置(我们用www.123.cn来进行配置),编辑以下配置文件server.xml,命令如下:

[root@tomcat ~]# vi /usr/local/tomcat/conf/server.xml ...... <Host name="www.123.cn" appBase="" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context path="" docBase="/data/wwwroot/123.cn/" debug="0" reloadable="true" crossContext="true"/> <Valve className="org.apache.catalina.valves.AccessLogValve." directory="logs" prefix="123.cn_access" suffix=".log" pattern="%h %l %u %t &quot;%r&quot;%s %b"/> </Host> ...... [root@tomcat ~]#

 valve为日志文件配置;  prefix定义访问日志的前缀;  suffix定义日志的后缀;  pattern定义日志格式。 注意:新增加的虚拟主机默认并不会生成类似默认虚拟主机的那个localhost.日期.log日志,错误日志会统一记录到catalina.out中。关于Tomcat日志,你最需要关注catalina.out,当出现问题时,我们应该第一想到去查看它。 配置完成后,重启Tomcat服务,命令如下:

[root@tomcat ~]# /usr/local/tomcat/bin/shutdown.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [root@tomcat ~]# /usr/local/tomcat/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@tomcat ~]#

重启Tomcat服务完成后,访问网站,查看/usr/local/tomcat/logs目录下是否有日志生成,并且查看生成的日志信息,命令如下:

[root@tomcat ~]# ls /usr/local/tomcat/logs/ 123_access_log.2020-03-29.txt localhost.2020-03-29.log catalina.2020-03-29.log localhost_access_log.2020-03-29.txt catalina.out manager.2020-03-29.log host-manager.2020-03-29.log [root@tomcat ~]# cat /usr/local/tomcat/logs/123_access_log.2020-03-29.txt 192.168.174.1 - - [29/Mar/2020:22:06:16 -0700] "GET / HTTP/1.1" 200 8645 192.168.174.1 - - [29/Mar/2020:22:06:16 -0700] "GET //favicon.ico HTTP/1.1" 200 9662 [root@tomcat ~]#

至此,Tomcat搭建配置完成。

二、主从同步服务

1.基础环境安装 (1)修改主机名 mysql1节点:

[root@localhost ~]# hostnamectl set-hostname mysql1 [root@localhost ~]# logout [root@mysql1 ~]# hostnamectl Static hostname: mysql1 Icon name: computer-vm Chassis: vm Machine ID: 179f6c8f2e7942ef81b0f5565a6883fa Boot ID: 69ad020d53e54892b9005f82e182c140 Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-327.el7.x86_64 Architecture: x86-64

mysql2节点:

[root@localhost ~]# hostnamectl set-hostname mysql2 [root@localhost ~]# logout [root@mysql2 ~]# hostnamectl Static hostname: mysql2 Icon name: computer-vm Chassis: vm Machine ID: 179f6c8f2e7942ef81b0f5565a6883fa Boot ID: 816b270a1275496caa3254300fc359c4 Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-327.el7.x86_64 Architecture: x86-64

(2)关闭防火墙及SELinux服务 两个节点关闭防火墙firewalld及SELinux服务,命令如下:

# setenforce 0 # systemctl stop firewalld

(3)配置hosts文件 两个节点配置/etc/hosts文件,修改为如下:

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.200.30 mysql1 192.168.200.40 mysql2

(4)配置YUM源并安装数据库服务

# yum install -y mariadb mariadb-server # systemctl start mariadb # systemctl enable mariadb Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

2.初始化数据库并配置主从服务 (1)初始化数据库 两个节点初始化数据库,配置数据库root密码为000000,命令如下:

[root@mysql1 ~]# mysql_secure_installation /usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): #默认按回车 OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] y New password: #输入数据库root密码000000 Re-enter new password: #再次输入密码000000 Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] n ... skipping. By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!

(2)配置mysql1主节点

[root@mysql1 ~]# cat /etc/my.cnf [mysqld] log_bin = mysql-bin #记录操作日志 binlog_ignore_db = mysql #不同步mysql系统数据库 server_id = 30 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.200.30,server_id就写30 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d

重启数据库服务,并进入数据库,命令如下:

[root@mysql1 ~]# systemctl restart mariadb [root@mysql1 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.44-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>

在mysql1节点,授权在任何客户端机器上可以以root用户登录到数据库,然后在主节点上创建一个user用户连接节点mysql2,并赋予从节点同步主节点数据库的权限。命令如下:

MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000"; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> grant replication slave on *.* to 'user'@'mysql2' identified by '000000'; Query OK, 0 rows affected (0.00 sec)

(3)配置mysql2从节点 修改mysql2节点的数据库配置文件,在配置文件/etc/my.cnf中的[mysqld]增添如下内容。

[root@mysql2 ~]# cat /etc/my.cnf [mysqld] log_bin = mysql-bin #记录操作日志 binlog_ignore_db = mysql #不同步mysql系统数据库 server_id = 40 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.200.40,server_id就写40 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d

在从节点mysql2上登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名mysql1,master_user为上一步中创建的用户user,命令如下:

[root@mysql2 ~]# systemctl restart mariadb [root@mysql2 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 10 Server version: 5.5.44-MariaDB MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> change master to master_host='mysql1',master_user='user',master_password='000000'; Query OK, 0 rows affected (0.01 sec)

配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用show slave status\G命令,并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。命令如下:

MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: mysql1 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 245 Relay_Log_File: mariadb-relay-bin.000005 Relay_Log_Pos: 529 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 245 Relay_Log_Space: 1256 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 30 1 row in set (0.00 sec)

可以看到Slave_IO_Running和Slave_SQL_Running的状态都是Yes,配置数据库主从集群成功。 3.验证数据库主从服务 (1)主节点创建数据库 先在主节点mysql1中创建库test,并在库test中创建表company,插入表数据,创建完成后,查看表company数据,命令如下:

[root@mysql1 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 5.5.44-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> create database test; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> use test; Database changed MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255)); Query OK, 0 rows affected (0.01 sec) MariaDB [test]> insert into company values(1,"alibaba","china"); Query OK, 1 row affected (0.01 sec) MariaDB [test]> select * from company; +----+---------+-------+ | id | name | addr | +----+---------+-------+ | 1 | alibaba | china | +----+---------+-------+ 1 row in set (0.00 sec)

(2)从节点验证复制功能 登录mysql2节点的数据库,查看数据库列表。找到test数据库,查询表,并查询内容验证从数据库的复制功能,命令如下:

[root@mysql2 ~]# mysql -uroot -p000000 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5 Server version: 5.5.44-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) MariaDB [(none)]> use test; 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 MariaDB [test]> show tables; +----------------+ | Tables_in_test | +----------------+ | company | +----------------+ 1 row in set (0.00 sec) MariaDB [test]> select * from company; +----+---------+-------+ | id | name | addr | +----+---------+-------+ | 1 | alibaba | china | +----+---------+-------+ 1 row in set (0.00 sec)

可以查看到主数据库中刚刚创建的库、表、信息,验证从数据库的复制功能成功。

三、MYSQL常用命令

MySQL常用操作 更改root密码 1.1更改环境变量 修改/etc/profile文件,文件尾添加mysql的绝对路径,如图1-1:

图1-1 修改环境变量 1.2创建MySQL密码 使用命令mysqladmin -uroot password '123456’为root用户创建初始密码,如图1-2所示:

如图1-2-1 创建初始密码 注释:可以忽略warning内容,指的是明码输入屏幕不安全。

使用命令mysql -uroot -p123456,完成初始密码登录,如图1-3所示:

如图1-2-1 使用初始密码完成登录

1.3 密码重置 修改配置文件/etc/my.cnf,在mysqld配置段,增加字段skip-grant,如图1-4所示:

图1-4: 修改配置文件配置段 修改完成后,重启mysql服务: /etc/init.d/mysqld restart 使用命令登入MySQL(修改的配置段,是完成忽略授权的操作,可以直接登入,无需输入用户名密码),切换到mysql库,对user表进行更新操作,如图所示1-5:

图1-5 重置命令 修改完成后,确认新密码登录有效。把/etc/my.cnf改回原有状态,并重启mysql服务。

连接MySQL 2.1 mysql -uroot -p123456

2.2 mysql -uroot -p123456 -h127.0.0.1 -P3306.

2.3 mysql -uroot -p123456 -S /tmp/mysql.sock

2.4 mysql -uroot -p123456 -e “show databases”

MySQL常用命令 3.1 查询库 show databases; 如下图所示:

3.2 切换库 use mysql; 如下图所示:

3.3 查看库里的表 show tables; 如下图所示:

3.4 查看表里的字段 desc tb_name; 如下图所示

3.5查看建表语句 show create table tb_name\G; 如下图所示:

3.6 查看当前用户 select user(); 如下图所示:

3.7查看当前使用的数据库 select database(); 如下图所示:

3.8 创建库 create database db1; 如下图所示:

3.9 创建表 use db1; create table t1(id int(4), name char(40)); 如下图所示:

3.10 查看当前数据库版本 select version(); 如下图所示:

3.11 查看数据库状态 show status; 如下图所示:

3.12 查看各参数 show variables; show variables like ‘max_connect%’; 如下图所示:

3.13修改参数 set global max_connect_errors=1000; 如下图所示:

3.14 查看队列 show processlist; show full processlist; 如下图所示:

MySQL创建用户以及授权 4.1 进行授权 grant all on . to ‘user1’ identified by ‘passwd’; grant SELECT,UPDATE,INSERT on db1.* to ‘user2’@‘192.168.133.1’ identified by ‘passwd’; grant all on db1.* to ‘user3’@’%’ identified by ‘passwd’;

4.2查看授权表 show grants;

show grants for user2@192.168.133.1;

5.MYSQL常用SQL语句 5.1 查看表内行数select count(*) from mysql.user; 如下图所示:

5.2 查看db表内的内容 select * from mysql.db; 如下图所示:

5.3 查看db表内含有db字段的内容 select db from mysql.db; 如下图所示:

5.4 搜索查看多个字段 select db,user from mysql.db; 如下图所示:

注释:搜索多个字段时,字段中间要用“,”隔开 5.5 查询host为127.0的内容 select * from mysql.db where host like ‘192.168.%’; 如下图所示:

5.6 向db1.t1中插入内容 insert into db1.t1 values (1, ‘abc’); 如下图所示:

5.7 把id=1的字段内容更新成aaa update db1.t1 set name=‘aaa’ where id=1; 如下图所示:

5.8 清空db1.t1表内的内容 truncate table db1.t1; 如下图所示:

注释:清空后表的结构依然存在 5.9 删除db1.t1表内的内容 drop table db1.t1; 如下图所示:

注释:清空后连同表的结构一同删除 5.10 清空db1.t1数据库 drop database db1; 如下图所示:

6.MySQL数据库的备份与恢复 6.1 备份库 mysqldump -uroot -p123456 mysql > /tmp/mysql.sql 6.2 恢复库 mysql -uroot -plinux mysql < /tmp/mysql.sql 6.3 备份表 mysqldump -uroot -plinux mysql user > /tmp/user.sql 6.4 恢复表 mysql -uroot -plinux mysql < /tmp/user.sql

图6-1 备份库、表与恢复库、表 6.5 备份所有库 mysql dump -uroot -p -A > /tmp/123.sql 6.6 只备份表结构 mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql

图6-2 备份所有库与备份表结构

最新回复(0)