暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

零基础安装MySQL 5.7数据库

DBA的辛酸事儿 2021-04-07
356

下载MySQL二进制软件包

下载连接:MYSQL安装包下载

官网MySQL有四个版本:GA版、DMR版、RC版、Beta版。一般生产和测试环境使用GA版(常规可用的版本,经过bug修复测试)


安装前系统环境检测

1、SELinux和系统防火墙iptables需要关闭

2、I/O调度系统默认是cfq模式,强烈建议使用deadline模式


cat sys/block/sda/queue/scheduler
#cfq完全公平队列;noop电梯式调度;deadline截止时间调度


3、swap分区的设置

不分配swap或分配4GB的swap

swappiness控制如何使用swap分区,0表示最大限度使用物理内存,然后才使用swap,可能导致内存溢出,导致MySQL被意外kill掉;100,积极使用swap,把内存数据及时搬到swap,不建议


cat proc/sys/vm/swappiness
编辑 etc/sysctl.conf 加入vm.swappiess的值


4、文件系统的选择 [xfs]

5、操作系统限制


ulinit -a
open files和max user processes,建议均设置为65535
vim etc/security/limits.conf
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535


6、numa需要关闭


numa --interleave=all usr/local/mysql/bin/mysqld_safe -defaults-file=/etc/my.cnf &


关闭numa,可以更好地分配内存,不需要采用swap的方式来获取内存

可以在BIOS、操作系统或者在数据库启动过程中关闭



开始进行安装


1、对下载下来的软件包进行MD5校验,确保下载过程中没有任何的问题


[root@wjq1 Packages]# md5sum mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
075dccd655e090ca999e2c8da3b67eb7  mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz


2、创建用户和组

3、将软件包移到/usr/local目录下


[root@wjq1 Packages]# mv mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz usr/local/

4、解压mysql软件包


[root@wjq1 Packages]# cd usr/local/
[root@wjq1 local]# tar zxvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

5、创建软连接,方便日后升级


[root@wjq1 local]# ln -s mysql-5.7.24-linux-glibc2.12-x86_64 mysql

6、创建mysql相关的目录并修改权限


[root@wjq1 ~]# mkdir -p data/mysql/3306/{data,log,tmp}
[root@wjq1 ~]# chown -R mysql:mysql usr/local/mysql
[root@wjq1 ~]# chown -R mysql:mysql data/mysql/

7、编辑mysql的配置文件


[root@wjq1 ~]# vim etc/my.cnf
[client]
port=3306
socket=/tmp/mysql3306.sock

[mysql]
prompt=\\U [\\d]>

[mysqld]
user=mysql
basedir = usr/local/mysql
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
character_set_server = utf8mb4

datadir=/data/mysql/3306/data
port=3306
server_id=330610
socket=/tmp/mysql3306.sock
log-output=file
slow_query_log = 1
long_query_time = 1
slow_query_log_file = data/mysql/3306/log/slow.log
log-error = data/mysql/3306/log/error.log
pid-file = data/mysql/3306/tmp/mysql3306.pid
binlog_format = mixed
log-bin = data/mysql/3306/log/mysql3306.bin

#gtid-mode = ON
#enforce-gtid-consistency = ON
#log-slave-updates = ON

8、初始化数据库


[root@wjq1 ~]# cd usr/local/mysql/bin/
[root@wjq1 bin]# ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql/3306/data --user=mysql --initialize
#--initialize参数,生成一个临时数据库初始密码,记录在log-error里面

9、启动数据库


[root@wjq1 bin]# ./mysqld_safe --defaults-file=/etc/my.cnf &
[1] 7481
[root@wjq1 bin]# 2018-11-05T07:10:36.482523Z mysqld_safe Logging to '/data/mysql/3306/log/error.log'.
2018-11-05T07:10:36.511901Z mysqld_safe Starting mysqld daemon with databases from data/mysql/3306/data

10、验证是否启动成功

[root@wjq1 bin]# netstat -tuplan | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      7739/mysqld


11、修改root用户的密码


[root@wjq1 ~]# cat /data/mysql/3306/log/error.log | grep password
2018-11-05T07:10:19.979308Z 1 [Note] A temporary password is generated for root@localhost: F?IJVgCaa1h4

[root@wjq1 bin]# ./mysql -u root -p                        
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-log

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.

root [(none)]>select user,host from mysql.user;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

root [(none)]>set password='XXXX@2019';
Query OK, 0 rows affected (0.00 sec)


MySQL数据库root密码丢失的问题


(1)kill掉mysql进程(共两个进程)

(2)重启mysql

./mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &


(3)连接到mysql

(4)设置新的密码并刷新权限

use mysql

update user set authentication_string=password('root123') where user='root';

flush privileges;

(5)重启数据库



↓点击阅读原文查看更多内容
文章转载自DBA的辛酸事儿,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论