CentOS7快速安装mysql5.7

(1)、安装前准备
创建mysql用户组,准备免安装版软件包,设置数据库目录权限
# groupadd mysql
# useradd mysql -g mysql
# wget https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
# tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
# mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql
# chown -R mysql:mysql /usr/local/mysql
# touch /var/log/mysql/mysql-error.log
# chown -R mysql:mysql /var/log/mysql/

(2)、编辑mysql主配置文件
如下mysql配置文件, 开启了二进制日志、慢查询日志、查询日志、错误日志
# cat /etc/my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[mysqld]
server-id=1
bind-address=0.0.0.0
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=20202
log_output=file
slow_query_log=on
slow_query_log_file=/usr/local/mysql/mysql-slow.log
log_queries_not_using_indexes=on
long_query_time=10
binlog_format=MIXED
log-bin=/usr/local/mysql/mysql-bin
expire_logs_days=15
max_binlog_size=500M
character-set-server=utf8
lower_case_table_names=1
# general_log=1
# general_log_file=/usr/local/mysql/mysql-query.log
pid-file=/usr/local/mysql/mysqld-pid
federated
max_connections=2000
character_set_server=utf8
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

# 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
show_compatibility_56=on

#
[client]
port=20202
socket=/tmp/mysql.sock
default-character-set=utf8

[mysqld_safe]
log-error=/var/log/mysql/mysql-error.log

# #
# # include all files from the config directory
# #
# !includedir /etc/my.cnf.d

(3)、初始化数据库
并记录初始化生成的root密码
# /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize // 初始化数据库

(4)、mysql服务配置
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
# service mysqld start
重启mysqld服务后,进行登入测试
# /usr/local/mysql/bin/mysql -u root -p
初始化后登入mysql会提示
" You must reset your password using ALTER USER statement before executing this statement. ”
需要使用下面的命令来修改密码
mysql> alter user user() identified by 'password';

MySQL 5.7 root远程授权
mysql> grant all privileges on *.* to root@'%' identified by 'password' with grant option;
mysql> flush privileges;

(5)、设置mysql命令软链接
编辑 /etc/profile 在文件末尾添加 export PATH=$PATH:/usr/local/mysql/bin
执行 source /etc/profile 刷新环境变量

(6)、关于mysql日志
mysql日志的种类,一般来说日志有五种,分别为:
错误日志:log-err (记录启动,运行,停止mysql时出现的信息)
二进制日志:log-bin (记录所有更改数据的语句,还用于复制,恢复数据库用)
查询日志:log (记录建立的客户端连接和执行的语句)
慢查询日志: log-slow-queries (记录所有执行超过long_query_time秒的所有查询)
更新日志: log-update (二进制日志已经代替了老的更新日志,更新日志在MySQL 5.1中不再使用)

查询当前日志记录的状况
mysql> show variables like 'log%';(是否启用了日志)
mysql> show master status; (怎样知道当前的日志)
mysql> show master logs; (显示二进制日志的数目)

mysql查询日志开启和关闭
mysql> set global general_log_file='/usr/local/mysql/mysql-query.log'; #设置路径
mysql> set global general_log=on; # 开启general log模式
mysql> set global general_log=off; # 关闭general log模式
MySQL的查询日志记录了所有MySQL数据库请求的信息。无论这些请求是否得到了正确的执行。默认文件名为hostname.log。默认情况下MySQL查询日志是关闭的。生产环境如果开启MySQL查询日志,对性能还是有蛮大的影响的。另外很多时候,MySQL慢查询日志基本可以定位那些出现性能问题的SQL,所以MySQL查询日志应用的场景其实不多,有点鸡肋的感觉,它跟SQL Server中的profiler有点类似,但是这个不能跟踪某个会话、用户、客户端。它只能对整个数据库进行跟踪。

(7)、 mysql最大连接数
mysql的最大连接数默认是100, 最大可以达到16384
1、查看最大连接数
mysql>show variables like '%max_connections%';
2、修改最大连接数
mysql> set GLOBAL max_connections = 2000;
3、最大连接数永久生效方法
在my.cnf配置文件中添加“max_connections=2000”
在修改最大连接数的时候会有这样一个疑问—这个值是不是越大越好,或者设置为多大才合适?这个参数的大小要综合很多因素来考虑,比如使用的平台所支持的线程库数量(windows只能支持到2048)、服务器的配置(特别是内存大小)、每个连接占用资源(内存和负载)的多少、系统需要的响应时间等。可以在global或session范围内修改这个参数。连接数的增加会带来很多连锁反应,需要在实际中避免由此引发的负面影响。
总体来说,该参数在服务器资源够用的情况下应该尽量设置大,以满足多个客户端同时连接的需求。否则将会出现类似”Too many connections”的错误。

(8)、mysql连接超时时间
Java程序员反应服务器上的MySQL连上去之后,总是过一会儿就断开连接了,工作效率低,可以增加连接时间,减少断开次数,和刷新页面的等待时间。
mysql> show global variables like '%timeout%';
设置全局变量connect_timeout为12小时(12*3600=43200)
mysql> SET GLOBAL connect_timeout = 43200;
mysql> exit;

相关新闻

联系我们

全国服务热线

400-033-9553

电子邮件:admin@example.com
工作时间:09:00-17:00 周一至周五

在线客服
关注微信
关注微信
分享本页
返回顶部