安装MySQL有3种方式:rpm包安装;源码安装;yum源安装(属于rpm包安装),本文讲解一下yum源安装。
参考资料:
下载各版本官网地址:https://downloads.mysql.com/archives/community/
yum库安装mysql快速指南:https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/#repo-qg-yum-installing
博客:https://www.cnblogs.com/luohanguo/p/9045391.html
博客:https://blog.csdn.net/z13615480737/article/details/78906598
1、配置MySQL的yum源,指定版本
vim /etc/yum.repos.d/mysql-community.repo
[mysql80-community] name=MySQL 8.0 Community Server baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ enabled=1 gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
注意:
1)如果是CentOS6版本的系统“/el/7/$basearch/”中的7要改为6。
2)enabled控制安装版本,enabled=0不安装,enabled=1安装 ,安装的是该版本中最新的。
3)gpgcheck=0,安装时不检测公钥、密钥,否则可能安装失败。
2、查看使用的版本
yum repolist enabled | grep mysql
yum list|grep mysql
3、执行安装命令
yum -y install mysql-community-server
4、启动
sudo service mysqld start
5、查看首次启动默认密码
grep 'temporary password' /var/log/mysqld.log
6、修改密码
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'SLei1234!';
注意:
1)默认情况下,至少包含一个大写字母,一个小写字母,一位数字和一个特殊字符,长度不少于8位。
2)先使用默认密码登录后再修改。
3)可以关闭validate_password插件,使用简单密码不做限制。(后面讲)
7、修改默认配置文件
vim /etc/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] # # 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 validate-password=OFF # 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 ~
注意:添加了语句:validate-password=OFF,取消验证密码,可以使用简单密码。
8、修改root密码为简单密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
9、给远程授权
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '123456';
flush privileges;
注意:flush privileges;命令有时可以不使用。