1.下载安装包(rpm下载)
1.1 下载地址的前缀是: wget http://dev.mysql.com/get/
1.2 后面拼接安装包名称即可Down MySQL Yum Repository
1.3 将名称拼接后完整下载命令是:
https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
1.4 mysql-5.7地址:
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
2.yum安装(yum repository)
2.1 安装下载过来的文件:yum -y install mysql57-community-release-el7-10.noarch.rpm
2.2 yum安装mysql: yum -y install mysql-community-server
2.3 启动mysql: service mysqld start
2.4 查看是否启动成功:service mysqld status
出现以下标准则代表启动成功
2.5 查看初始密码:
grep "password" /var/log/mysqld.log
2.6 进入数据库:
mysql -uroot -p
这里到-p就行了,回车会提示你输入密码的2.7 修改初始密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
注意密码需要有大小写字母数字2.8 授权root账号远程登录:
grant all on *.* to root@'%' identified by '新密码';
2.8.1 MySQL 8.0已经不支持2.8这种写法,如下:
//正确的写法是先创建用户
CREATE USER 'root'@'%' IDENTIFIED BY '新密码';
//然后在给用户授权
grant all privileges on *.* to 'root'@'%' ;
2.8.2 刷新命令:
flush privileges;
2.9 mysql 重要目录说明:
#(a)数据库目录
/var/lib/mysql/
#(b)配置文件
/usr/share /mysql(mysql.server命令及配置文件)
#(c)相关命令
/usr/bin(mysqladmin mysqldump等命令)
#(d)启动脚本
/etc/rc.d/init.d/(启动脚本文件mysql的目录)
mysql配置文件: /etc/my.cnf
3.下载安装包方式rpm-tar(5.7)
3.1.下载rmp tar文件
5.7.22
版本号可以更改为指定的
5.7.22:wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-
5.7.22-1.el7.x86_64.rpm-bundle.tar
5.7.30:wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-
5.7.30-1.el7.x86_64.rpm-bundle.tar
3.2.解压:tar -xvf mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar
得到:
3.3 查看已安装的mysql:
rpm -qa|grep mysql
如图:
3.4 查看mariadb:
rpm -qa|grep mariadb
//rpm 删除指定包
rpm -e --nodeps 包名
如:rpm -e --nodeps mysql-community-common-5.7.30-1.el7.x86_64
//安装指定包
rpm -ivh 包
如:rpm -ivh mysql-community-common-5.7.30-1.el7.x86_64.rpm
3.5 配置参考2.5
步骤一致,my.cnf
文件
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
log-bin=mysql-bin #开启二进制日志
server-id=31 #设置server-id
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
binlog-do-db=imm
#
# 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
#
# 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 = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
explicit_defaults_for_timestamp=true
character_set_server=utf8
init_connect='SET NAMES utf8'
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
validate_password_policy=0
validate_password = off
max_connections=1000
mysql 8.0 my.cnf配置
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
log-bin=mysql-bin #开启二进制日志
server-id=31 #设置server-id
#
# 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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# 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 = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4' # 设置client连接mysql时的字符集,防止乱码
default-storage-engine=INNODB
lower_case_table_names = 1 # 是否对sql语句大小写敏感,1表示不敏感
explicit_defaults_for_timestamp = true # TIMESTAMP如果没有显示声明NOT NULL,允许NULL值
max_connections = 1000
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
4.Linux yum 安装,再次初始化数据库及密码
4.1:首先停止服务:systemctl stop mysqld
4.2:删除默认的数据文件:rm -rf /var/lib/mysql/*
4.3:删除相关日志:rm -rf /var/log/mysql*
4.4:根据配置文件,创建日志文件
touch /var/log/mysqld.log
chown mysql.mysql /var/log/mysqld.log
二进制日志文件路径(从服务不需要做此步骤)
mkdir /var/log/mysql
chown mysql.mysql /var/log/mysql
4.5:执行初始化命令
指定用户为mysql
,是否对sql语句大小写敏感,1表示不敏感
mysqld --initialize --user=mysql --lower_case_table_names=1
4.6:启动 mysqld:systemctl start mysqld
4.7:查询初始化后 root 的密码:grep temporary /var/log/mysqld.log
ps:日志文件必须授权为mysql用户,初始化时指定user,否则启动时会报Could not open file '/var/log/mysqld.log' for error logging: Permission denied