- 安装前准备
- 一 安装前准备
- 1、检查是否已经安装过mysql,执行命令
- 2、查询所有Mysql对应的文件夹
- 3、检查mysql用户组和用户是否存在,如果没有,则创建
- 4、从官网下载是用于Linux的Mysql安装包
- 二 安装Mysql
- 1、在执行wget命令的目录下或你的上传目录下找到Mysql安装包:mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
- 2、在/usr/local/mysql目录下创建data目录
- 3、更改mysql目录下所有的目录及文件夹所属的用户组和用户,以及权限
- 4、编译安装并初始化mysql,务必记住初始化输出日志末尾的密码(数据库管理员临时密码)
- 5、运行初始化命令成功后,输出日志如下:
- 6、编辑配置文件my.cnf,添加配置如下
- 7、测试启动mysql服务器
- 7. 登陆,修改密码
- 8. 开启Navicat远程连接
- 9.navicat连接成功
- 定时备份
安装前准备
一 安装前准备
1、检查是否已经安装过mysql,执行命令
[root@localhost /]# rpm -qa | grep mysql
从执行结果,可以看出我们已经安装了mysql-libs-5.1.73-5.el6_6.x86_64,执行删除命令
[root@localhost /]# rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64
再次执行查询命令,查看是否删除
[root@localhost /]# rpm -qa | grep mysql
2、查询所有Mysql对应的文件夹
[root@localhost /]# whereis mysqlmysql: /usr/bin/mysql /usr/include/mysql[root@localhost lib]# find / -name mysql/data/mysql/data/mysql/mysql
删除相关目录或文件
[root@localhost /]# rm -rf /usr/bin/mysql /usr/include/mysql /data/mysql /data/mysql/mysql
验证是否删除完毕
[root@localhost /]# whereis mysqlmysql:[root@localhost /]# find / -name mysql[root@localhost /]#
3、检查mysql用户组和用户是否存在,如果没有,则创建
[root@localhost /]# cat /etc/group | grep mysql[root@localhost /]# cat /etc/passwd |grep mysql[root@localhost /]# groupadd mysql[root@localhost /]# useradd -r -g mysql mysql[root@localhost /]#
4、从官网下载是用于Linux的Mysql安装包
下载命令:
[root@localhost /]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
也可以直接到 mysql官网 选择对应版本进行下载。
下载安装包
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.12-linux-glibc2.12-x86_64.tar.xz

解压
xz -d mysql-8.0.12-linux-glibc2.12-x86_64.tar.xz
tar -xvf mysql-8.0.12-linux-glibc2.12-x86_64.tar
二 安装Mysql
1、在执行wget命令的目录下或你的上传目录下找到Mysql安装包:mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
执行解压命令:
[root@localhost /]# tar xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
解压完成后,可以看到当前目录下多了一个解压文件,移动该文件到/usr/local/下,并将文件夹名称修改为mysql。
如果/usr/local/下已经存在mysql,请将已存在mysql文件修改为其他名称,否则后续步骤可能无法正确进行。
执行命令如下:
[root@localhost /]# mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/[root@localhost /]# cd /usr/local/[root@localhost /]# mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql
如果/usr/local/下不存在mysql文件夹,直接执行如下命令,也可达到上述效果。
[root@localhost /]# mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql
2、在/usr/local/mysql目录下创建data目录
[root@localhost /]# mkdir /usr/local/mysql/data
3、更改mysql目录下所有的目录及文件夹所属的用户组和用户,以及权限
[root@localhost /]# chown -R mysql:mysql /usr/local/mysql[root@localhost /]# chmod -R 755 /usr/local/mysql
4、编译安装并初始化mysql,务必记住初始化输出日志末尾的密码(数据库管理员临时密码)
[root@localhost /]# cd /usr/local/mysql/bin[root@localhost bin]# ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
补充说明:
第4步时,可能会出现错误:
出现该问题首先检查该链接库文件有没有安装使用 命令进行核查
[root@localhost bin]# rpm -qa|grep libaio[root@localhost bin]#
运行命令后发现系统中无该链接库文件
[root@localhost bin]# yum install libaio-devel.x86_64
安装成功后,继续运行数据库的初始化命令,此时可能会出现如下错误:
执行如下命令后:
[root@localhost bin]# yum -y install numactl
执行无误之后,再重新执行第4步初始化命令,无误之后再进行第5步操作!
5、运行初始化命令成功后,输出日志如下:

记录日志最末尾位置root@localhost:后的字符串,此字符串为mysql管理员临时登录密码。
6、编辑配置文件my.cnf,添加配置如下
[root@localhost bin]# vi /etc/my.cnf[mysqld]# Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.# innodb_buffer_pool_size = 128M# Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin# These are commonly set, remove the # and set as required.basedir = /usr/local/mysqldatadir = /usr/local/mysql/datasocket = /tmp/mysql.socklog-error = /usr/local/mysql/data/error.logpid-file = /usr/local/mysql/data/mysql.pidport = 3306#lower_case_table_names = 1# server_id = .....# socket = .....#lower_case_table_names = 1max_allowed_packet=32Mdefault-authentication-plugin = mysql_native_password#lower_case_file_system = on#lower_case_table_names = 1log_bin_trust_function_creators = ON# Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed, experiment to find the optimal values.# join_buffer_size = 128M# sort_buffer_size = 2M# read_rnd_buffer_size = 2Msql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names:是否区分大小写,1表示存储时表名为小写,操作时不区分大小写;0表示区分大小写;不能动态设置,修改后,必须重启才能生效:character_set_server:设置数据库默认字符集,如果不设置默认为latin1innodb_file_per_table:是否将每个表的数据单独存储,1表示单独存储;0表示关闭独立表空间,可以通过查看数据目录,查看文件结构的区别;
7、测试启动mysql服务器
[root@localhost /]# /usr/local/mysql/support-files/mysql.server start
显示如下结果,说明数据库安装并可以正常启动
8、
[root@localhost /]# ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql[root@localhost /]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql[root@localhost /]# ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump[root@localhost /]# service mysql restart
7. 登陆,修改密码
密码:第4步初始化数据库随机生成得密码,输入密码不显示。
[root@VM_0_3_centos support-files]# service mysql startStarting MySQL.. SUCCESS![root@VM_0_3_centos support-files]# mysql -uroot -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 82Server version: 8.0.20Copyright (c) 2000, 2020, 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> alter user 'root'@'localhost' identified by 'kid+1412';Query OK, 0 rows affected (0.01 sec)mysql>
8. 开启Navicat远程连接
# 登录[root@VM_0_3_centos support-files]# mysql -uroot -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2582Server version: 8.0.20 MySQL Community Server - GPLCopyright (c) 2000, 2020, 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数据库mysql> use mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changed# 查看用户信息mysql> select host, user, authentication_string, plugin from user;+-----------+------------------+------------------------------------------------------------------------+-----------------------+| host | user | authentication_string | plugin |+-----------+------------------+------------------------------------------------------------------------+-----------------------+| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password || localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password || localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password || localhost | root | $A$005$=!..iRO=k:x_>qiFtXTYJmwgur/2pJH1mfk26FBL.1YZOC.YcBw8auuFFM0 | caching_sha2_password |+-----------+------------------+------------------------------------------------------------------------+-----------------------+4 rows in set (0.00 sec)# 更新root信息mysql> update user set host = '%' where user = 'root';Query OK, 1 row affected (0.02 sec)Rows matched: 1 Changed: 1 Warnings: 0# 授权root用户可以远程登陆,失败mysql> GRANT ALL ON *.* TO 'root'@'%';ERROR 1410 (42000): You are not allowed to create a user with GRANT# 立即生效,重新授权mysql> flush privileges;Query OK, 0 rows affected (0.02 sec)# 授权root用户可以远程登陆mysql> GRANT ALL ON *.* TO 'root'@'%';Query OK, 0 rows affected (0.02 sec)# 立即生效mysql> flush privileges;Query OK, 0 rows affected (0.01 sec)#mysql> alter user 'root'@'%' identified with mysql_native_password by 'kid+1412';Query OK, 0 rows affected (0.01 sec)# 立即生效mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)# 退出mysql> exit
9.navicat连接成功

定时备份
#!/bin/bash#保存备份个数,备份31天数据number=31#备份保存路径backup_dir=/app/databack/database#日期dd=`date +%Y-%m-%d-%H-%M-%S`#备份工具tool=mysqldump#用户名username=root#密码password=123456#将要备份的数据库database_name=ry#如果文件夹不存在则创建if [ ! -d $backup_dir ];thenmkdir -p $backup_dir;fi#简单写法 mysqldump -u root -p123456 users > /root/mysqlbackup/users-$filename.sql$tool -u $username -p$password $database_name > $backup_dir/$database_name-$dd.sql#写创建备份日志echo "create $backup_dir/$database_name-$dd.dupm" >> $backup_dir/log.txt#找出需要删除的备份delfile=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | head -1`#判断现在的备份数量是否大于$numbercount=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | wc -l`if [ $count -gt $number ]then#删除最早生成的备份,只保留number数量的备份rm $delfile#写删除文件日志echo "delete $delfile" >> $backup_dir/log.txtfi
启动 定时任务 
查看定时任务
crondtab -l
编辑定时任务
crontab -u root -e
输入:
00 03 * * * /app/databack/mysql_back.sh:wq
出现该问题首先检查该链接库文件有没有安装使用 命令进行核查
执行如下命令后:
