二进制安装
1) 安装依赖包
yum install ncurses-devel libaio-devel -y
2)创建用户
groupadd mysql -g 666useradd mysql -u 666 -g 666-s /sbin/nologin -M
3)解压
mkdir /application mkdir /server/tools -p cd /server/tools/ tar xf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.20-linux-glibc2.12-x86_64 /application/mysql-5.7.20
4)创建日志
mkdir /application/mysql-5.7.20/data #用于存放数据库数据 mkdir /application/mysql-5.7.20/tmp #用于存放socket文件 ln -s /application/mysql-5.7.20/ /application/mysql touch /application/mysql-5.7.20/mysql.log
5)初始化数据
#初始化数据库命令 /application/mysql-5.7.20/bin/mysqld --initialize --initialize --user=mysql --basedir=/application/mysql-5.7.20 --datadir=/application/mysql-5.7.20/data #这里会生成MySQL第一次登录使用的临时密码,需要保存下来 2019-01-13T16:11:48.234884Z 1 [Note] A temporary password is generated for root@localhost: ,rK+/7/AvOF: 参数解释: --initialize #初始化mysql --user=mysql #指定mysql用户 --basedir #指定mysql基础目录 --datadir #指定mysql数据存放目录
6)授权
chown -R mysql.mysql /application/mysql-5.7.20
7)添加配置文件
#1.修改启动脚本内容中的路径 cd /application/mysql/support-files/ sed -i 's#/usr/local#/application/mysql#g' mysql.server cp mysql.server /etc/init.d/mysqld #2.配置my.cnf [root@db01 /]# vim /etc/my.cnf [mysqld] basedir=/application/mysql datadir=/application/mysql/data socket=/application/mysql/tmp/mysql.sock character-set-server=utf8 port=3306 server_id=10 log-error=/application/mysql/mysql.log log-bin=/application/mysql/data/mysql-bin binlog_format=row skip_name_resolve [mysql] socket=/application/mysql/tmp/mysql.sock [mysqladmin] socket=/application/mysql/tmp/mysql.sock #3.启动mysql /etc/init.d/mysqld start
8)配置环境变量
echo 'export "PATH=/application/mysql/bin:$PATH"' >>/etc/profile source /etc/profile
9)修改密码
[root@db01 /]# mysql -uroot -p,rK+/7/AvOF: [root@db01 /]# mysqladmin -uroot -p password 123456 Enter password: #输入旧密码
源码安装
1)依赖包
yum install ncurses-devel libaio-devel cmake -y
2)创建用户
groupadd mysql -g 666useradd mysql -u 666 -g 666 -s /sbin/nologin -M
3)安装目录
mkdir /server/tools -p mkdir /application -p
4)编译
cd /server/tools/ rz mysql-5.7.20.tar.gz [root@db01 tools]# tar xf mysql-5.7.20.tar.gz [root@db01 tools]# cd mysql-5.7.20/ [root@db02 mysql-5.7.20]# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.7.20 \ -DMYSQL_DATADIR=/application/mysql-5.7.20/data \ -DMYSQL_UNIX_ADDR==/application/mysql-5.7.20/tmp/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DMYSQL_TCP_PORT=3306 \ -DMYSQL_USER=mysql \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DENABLE_DOWNLOADS=1 \ -DDOWNLOAD_BOOST=1 \ -DWITH_BOOST=/usr/local/boost [root@db01 mysql-5.6.36]# make && make install 编译参数解释: -DCMAKE_INSTALL_PREFIX #MySQL安装的路径 -DMYSQL_DATADIR #存放MySQL数据的路径 -DMYSQL_UNIX_ADDR #MyQSL启动后的sock存放路径 -DDEFAULT_CHARSET #MySQL数据库中的字符集 -DWITH_BOOST #mysql源码中用到了C++的Boost库 -DENABLE_DOWNLOADS #例如boost路径填/usr/local/boost,它会在这个目录找,找不到就会自己下载到该目录
5)目录介绍
[root@db01 mysql-5.7.20]# tree -L 1 /application/mysql-5.7.20/ /application/mysql-5.7.20/ ├── bin #存放mysql命令 ├── COPYING ├── COPYING-test ├── docs #自带帮助文件 ├── include ├── lib #lib库 ├── man ├── mysql-test ├── README ├── README-test ├── share └── support-files
6)配置文件
#1.创建软连接 [root@db01 /]# ln -s /application/mysql-5.7.20/ /application/mysql #2.配置/etc/my.cnf(5.7版本默认不自带my.cnf模板文件) [root@db01 /]#vim /etc/my.cnf [mysqld] basedir=/application/mysql datadir=/application/mysql/data socket=/application/mysql/tmp/mysql.sock character-set-server=utf8 port=3306 server_id=10 log-error=/application/mysql/mysql.log log-bin=/application/mysql/data/mysql-bin binlog_format=row skip_name_resolve [client] socket=/application/mysql/tmp/mysql.sock #3.创建/application/mysql/tmp目录用于存放mysql sock文件,并设置mysql目录权限 [root@db01 ~]# mkdir /application/mysql/tmp -p [root@db01 ~]# chown -R mysql.mysql /application/mysql/ #4.创建错误日志文件 [root@db01 ~]# touch /application/mysql-5.7.20/mysql.log #5.初始化数据库(所有的初始化信息都保存在mysql日志文件中,包括初始化的root密码) [root@db01 ~]# mkdir /application/mysql-5.7.20/data [root@db01 ~]# /application/mysql-5.7.20/bin/mysqld --initialize --user=mysql --basedir=/application/mysql-5.7.20 --datadir=/application/mysql-5.7.20/data #6.复制mysql启动脚本至/etc/init.d/mysqld,该脚本用于mysql启动,重启以及关闭 [root@db01 ~]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld #7.启动mysql数据库,mysql默认端口为3306 [root@db01 ~]# /etc/init.d/mysqld start [root@db01 ~]# netstat -lntup |grep 3306 #8.增加mysql环境变量,为了可以直接在命令行执行mysql命令 [root@db01 ~]# echo 'export "PATH=/application/mysql/bin:$PATH"' >>/etc/profile [root@db01 ~]# tail -1 /etc/profile [root@db01 ~]# source /etc/profile [root@db01 ~]# echo $PATH
7)修改密码
[root@db01 /]# grep "temporary password" /application/mysql/mysql.log 2019-01-14T03:22:24.841670Z 1 [Note] A temporary password is generated for root@localhost: H#hbzsuPl1/- [root@db01 /]# mysqladmin -uroot -p password "123456" Enter password: #输入旧密码进行更改 [root@db01 /]# mysql -uroot -p123456 #输入新密码进入数据库
Yum安装
1)安装目录
mkdir /server/tools -p cd /server/tools/
2)下载对应源
wget http://repo.mysql.com//mysql57-community-release-el7-8.noarch.rpm
3)安装
yum -y install mysql57-community-release-el7-8.noarch.rpm yum -y install mysql-community-server.x86_64
4)启动
systemctl start mysqldsystemctl enable mysqldnetstat -lntup |grep 3306
5)修改密码
# mysql安装完成后,会在/var/log/mysqld.log文件中给root生成一个默认的登录密码 [root@db01 /]# grep "temporary password" /var/log/mysqld.log 2019-01-14T02:04:55.465890Z 1 [Note] A temporary password is generated for root@localhost: al_gF&9eZUjW #进入mysql命令行,修改密码为:MyNewPass4! [root@db01 /]# mysql -uroot -p"al_gF&9eZUjW" mysql> set password = password('MyNewPass4!'); 注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位
6)修改mysql组策略
#1.查看系统密码策略 [root@db01 /]# mysql -uroot -pMyNewPass4! -e "show variables like '%password%';" |grep "length" validate_password_length 8 #2.在/etc/my.cnf配置文件中添加validate_password_policy配置,指定密码策略 [mysqld] #validate_password_policy=0 #指定密码策略 0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件 validate_password = off #禁用密码策略 #重新启动mysql服务使配置生效: systemctl restart mysqld #再次修改mysql密码 [root@db01 /]# mysqladmin -uroot -p password 123456 Enter password: #输入旧密码MyNewPass4!完成更改
默认配置文件
/etc/my.cnf #配置文件/var/log/mysqld.log #日志文件/var/run/mysqld/mysqld.pid #socket文件
忘记密码如何登陆?
#在[mysqld]的段中加上一句:skip-grant-tables 保存并且退出vi。 mysql -u root update mysql.user set authentication_string=password('123qwe') where user='root' and Host = 'localhost'; flush privileges