MySQL的安装与配置

Posted mingyuewu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL的安装与配置相关的知识,希望对你有一定的参考价值。

mysql的安装与配置

时间2021-11-19
环境centos7.9 mysql5.7

一、下载MySQL rpm包

https://downloads.mysql.com/archives/community/

选择版本

下载server,client,common, libs 4个rpm

二、安装MySql

1 检查删除旧的Mysql

rpm -q mariadb-libs
rpm -e mariadb-libs-5.5.64-1.el7.x86_64 --nodeps

2 添加mysql用户及用户组

groupadd  mysql
useradd  mysql  -g mysql

3 rpm安装mysql

# 将4个rpm包放在一个目录下
rpm -ivh  mysql-community-*  

或者按照 common  lib  client  server的顺序一个一个安装
后者安装的时候加参数  --force --nodeps 

4 确认安装完毕

#查看有4个rmp包
rpm -qa | grep mysql

5 mysql的安装目录

6 MySQL的启停

systemctl start mysqld
systemctl stop mysqld

三、登录Mysql

1 找MySQL的初始密码

# mysql5.7安装时设置了root初始密码,查询下
grep 'temporary password' /var/log/mysqld.log

2 登录MySQL

mysql -hlocalhost -P3306 -uroot  -p
输入密码

3 修改MySQL密码

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

4 忘记MySQL密码,修改密码安全等级等

https://blog.csdn.net/qq_44817119/article/details/121253080

四、MySql配置文件my.cnf

1 路径

/etc/my.cnf 或者location my.cnf

2 my.cnf结构

MySQL配置文件中的启动选项被划分为若干个,每个组下面可以定义若干个选项。

用于MySql服务器

[server] 作用于所有的服务器程序

[mysqld]

[mysqld_safe]

用于MySql客户端

[client]作用于所有的客户端程序

[mysql]
[mysqladmin]
[mysqldump]

[server]
[mysqld]  
[mysqld_safe]
[client]
[mysql]
[mysqladmin]
[mysqldump]

3 一个具体的MySQL配置文件举例

####指定客户端连接服务端时的端口,及socket文件位置
#客户端和服务器端应约定相同的socket,不然会找不到socket
# 或者客户端在登录时,在命令行中 --socket=/var/lib/mysql/mysql.sock
[client]
port = 3306
socket=/var/lib/mysql/mysql.sock

[mysqld]
# 端口及路径
port = 3306   
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysqld/mysqld.pid
basedir=/usr/local/mysql
datadir=/data/mysql/data
log-error=/var/log/mysqld.log
# 字符集、server-id、默认存储引擎
character-set-server = utf8
server-id = 1
default-storage-engine = InnoDB
# 连接数与innodb
max_connections = 600  
#连接数是什么,什么时候需要建立连接,用户数与连接数,线程与连接数,连接池,连接向数据库发送SQL语句,返回执行结果。
wait_timeout=1800  #mysql连接空闲1800秒后关闭,默认8小时
max_user_connections=20  #一个数据库用户的最大连接 0表示不设限制
thread_concurrency=8   # 设置为CPU核数的2倍
innodb_buffer_pool_size = 8G  # 物理内存一半
innodb_file_per_table=1  #把每个表的数据单独保存,以前是都保存在系统表空间
innodb_log_file_size=256M   #Redo log空间大小
innodb_flush_log_at_trx_commit=2
# 线程私有内存
join_buffer_size=8M
sort_buffer_size=8M
tmp_table_size=8M
read_rnd_buffer_size=8M
# bin_log
log-bin= /data/mysql/logs/binlog
binlog_format=row
sync_binlog=10
# 慢日志
slow_query_log=ON
long_query_time = 1
slow_query_log_file = /data/mysql/logs/mysql-slow.log

[mysqldump]
quick
max_allowed_packet = 16M

部分参数详解:

参考文档
https://docs.oracle.com/cd/E17952_01/mysql-5.7-en/mysql-5.7-en.pdf

innodb_flush_log_at_trx_commit

innodb_flush_log_at_trx_commit
 
1   默认值   遵从ACID原则, Logs are written and flushed to disk at
each transaction commit.

0   logs are written and flushed to disk once per second. Transactions for which logs
have not been flushed can be lost in a crash.

2  logs are written after each transaction commit and flushed to disk once per second. Transactions for which logs have not been flushed can be lost in a crash

#  question   writen  和  flush 区别 写缓存和写磁盘吗

sync_binlog

Controls how often the MySQL server synchronizes the binary log to disk.
• sync_binlog=0: Disables synchronization of the binary log to disk by the MySQL server. Instead,
the MySQL server relies on the operating system to flush the binary log to disk from time to time as it
does for any other file. This setting provides the best performance, but in the event of a power failure
or operating system crash, it is possible that the server has committed transactions that have not been
synchronized to the binary log.
• sync_binlog=1: Enables synchronization of the binary log to disk before transactions are
committed. This is the safest setting but can have a negative impact on performance due to
the increased number of disk writes. In the event of a power failure or operating system crash,
transactions that are missing from the binary log are only in a prepared state. This permits the
automatic recovery routine to roll back the transactions, which guarantees that no transaction is lost
from the binary log.
• sync_binlog=N, where N is a value other than 0 or 1: The binary log is synchronized to disk after
N binary log commit groups have been collected. In the event of a power failure or operating system
crash, it is possible that the server has committed transactions that have not been flushed to the
binary log. This setting can have a negative impact on performance due to the increased number of
disk writes. A higher value improves performance, but with an increased risk of data loss

五、基本SQL语句

1 查看数据库状态

#  系统变量
show   variables  like  'default_storage_engine';

show   variables  like  'max_connections';

#show processlist,如果经常发现MYSQL中有大量的Sleep进程,则需要修改wait_timeout值
show variables like 'wait_timeout';

show variables like 'thread_concurrency';

show   variables  like  'default%';

#  状态变量
show  processlist;

show  status  like 'Threads-connected' ;  #当前有多少客户端与服务器建立了连接

show  status  like 'Innodb_rows_updated' ; #更新了多少条以InnoDB为存储引擎的表中的记录

show   status  like 'thread%';

2 创建数据库,表,用户等

http://c.biancheng.net/view/2430.html

以上是关于MySQL的安装与配置的主要内容,如果未能解决你的问题,请参考以下文章

从零开始配置vim(27)——代码片段

Linux(DeepInOS) 下 mysql 的安装与基本配置

Linux MySql 安装与配置

#VSCode保存插件配置并使用 gist 管理代码片段

LAMP的基本配置

macOS 下的 MySQL 8.0.17 安装与简易配置