1、实验环境
服务器类型:VMware虚拟机
服务器操作系统:CentOS-7.5
服务器名称:CentOS-1
服务器IP:192.168.218.128
防火墙+selinux均已关闭
网络连通状况:服务器可以上外网
2、MySQL-5.7 安装源的下载
1)先卸载系统自带的mariadb
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# rpm -qa mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# rpm -qa mariadb
[root@CentOS-1 ~]#
2)下载并安装mysql-5.7的安装源
yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm #安装mysql-5.7的安装源
[root@CentOS-1 ~]# ll /etc/yum.repos.d/ | grep 'mysql-community*'
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# yum list mysql-community-server
3、删除Linux系统原来自带的 my.cnf 文件(如果存在的话)
[root@CentOS-1 ~]# rm -rf /etc/my.cnf
[root@CentOS-1 ~]#
4、安装 MySQL-5.7
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# yum -y install mysql-community-server #安装mysql
[root@CentOS-1 ~]# rpm -q mysql-community-server
mysql-community-server-5.7.29-1.el7.x86_64
[root@CentOS-1 ~]#
5、启动MySQL-5.7
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# systemctl start mysqld.service #启动mysql
[root@CentOS-1 ~]# systemctl enable mysqld.service #设置mysql服务为自启动
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# lsof -i:3306
[root@CentOS-1 ~]#
6、修改配置文件
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# mkdir /var/lib/mysql/binary_log #创建用于存放binlog日志的目录'binary_log'
[root@CentOS-1 ~]# ls /var/lib/mysql/binary_log
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# ll /var/lib/mysql | grep 'binary_log' #查看'binary_log'目录的权限
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# chown -R mysql.mysql /var/lib/mysql/binary_log #修改该目录权限
[root@CentOS-1 ~]# ll /var/lib/mysql | grep 'binary_log'
[root@CentOS-1 ~]# vim /etc/my.cnf #编辑修改mysql服务器的配置文件
说明:因为后期要配置主从同步,所以配置文件中设置server-id,以及binlog存储文件以master_a-binlog开头(master_a表示第一个主服务器)
[root@CentOS-1 ~]# cat /etc/my.cnf #查看修改后的配置文件
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# systemctl restart mysqld.service #重启mysql服务,使配置生效
[root@CentOS-1 ~]#
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# ll /var/lib/mysql/binary_log/ #查看此时是否生成了binlog文件
7、MySQL登录测试
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# grep 'temporary' /var/log/mysqld.log #查看系统分配的mysql临时登录密码
[root@CentOS-1 ~]# mysql -uroot -p'piDr2,8x#W6S' #使用临时密码登录mysql并做查询操作,发现查询失败
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# mysql -uroot -p'piDr2,8x#W6S' #再次登录mysql,根据错误提示进行排错
mysql>
mysql> show databases; #查看错误提示
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mysql@123'; #根据错误提示进行排错
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> flush privileges; #刷新权限
Query OK, 0 rows affected (0.00 sec)
mysql>
8、使用安全配置向导设置MySQL(MySQL初始化)
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# mysql_secure_installation #安全配置向导
Securing the MySQL server deployment.
Enter password for user root: #输入root用户密码'Mysql@123'
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100 #预计密码强度为100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y #更改root用户密码(是或者否)
New password: #输入新root密码,此处新密码设置为“Mysql.2020”
Re-enter new password: #再次输入新root密码“Mysql.2020”
Estimated strength of the password: 100 #预计密码强度为100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y #您是否要继续使用提供的root密码(是或者否)
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y #是否删除匿名用户
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y #禁止root用户从远程登录mysql服务器
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y #是否删除测试数据库
-
Dropping test database...
Success. - Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y #现在重新加载授权表
Success.
All done!
[root@CentOS-1 ~]#
9、MySQL登录
[root@CentOS-1 ~]#
[root@CentOS-1 ~]# mysql -uroot -p'Mysql.2020'
来源:51CTO
作者:wx5e86f96956311
链接:https://blog.51cto.com/14783377/2485030