在CentOS中默认安装有MariaDB,而我们需要用的mysql,所有需要将其覆盖掉,下面是安装过程:
1、下载并安装MySQL官方的 Yum Repository
[root@VM-0-7-centos ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
2、安装MySQL服务器。
[root@VM-0-7-centos ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm[root@VM-0-7-centos ~]# yum -y install mysql-community-server
出现如下图所示,则表示安装成功:
3、首次启动mysql
[root@VM-0-7-centos ~]# systemctl start mysqld.service
4、查看mysql运行状态
[root@VM-0-7-centos ~]# systemctl status mysqld.service
5、设置开机自启动
[root@VM-0-7-centos ~]# systemctl enable mysqld
6、卸载Yum Repository
但此时还有一个问题,就是因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:
[root@VM-0-7-centos ~]# yum -y remove mysql57-community-release-el7-10.noarch
至此,mysql的安装已经全部完毕,下面进行mysql的一些操作。
1、获取mysql初始密码
安装好mysql之后,输入mysql就可以进入mysql,但是mysql默认帮我设置了初始密码,导致我们进不去,如下图所示
此时,我们可以查看mysql安装的日志,找到初始密码,如下图所示:
[root@VM-0-7-centos ~]# cat /var/log/mysqld.log
或者通过grep快速找到初始密码,如下所示
[root@VM-0-7-centos ~]# cat /var/log/mysqld.log |grep "password"2021-01-03T07:58:49.809313Z 1 [Note] A temporary password is generated for root@localhost: D.hV:nDQi3RU2021-01-03T08:05:40.815394Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)2021-01-03T08:14:45.549773Z 3 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2、进入mysql
输入mysql -u root -p 后再输入初始密码即可进入mysql,如下图所示,接下就可以创建数据库、创建表等一系列操作了。
[root@VM-0-7-centos ~]# mysql -u root -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 12Server version: 5.7.32Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
3、退出mysql
mysql> exit;Bye[root@VM-0-7-centos ~]#
4、进入mysql修改密码
输入mysql -u root -p 后再输入初始密码即可进入mysql,输入
set password for root @localhost = password('ABCDabcd1234.?')
即可修改密码为ABCDabcd1234.?,如下图所示
[root@VM-0-7-centos ~]# mysql -u root -pEnter password:Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> set password for root @localhost = password('ABCDabcd1234.?');Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> exit;Bye
5、配置mysql的root用户允许远程登录
基于安全考虑root账户一般只能本地访问(新安装的数据库只能localhost访问),但是在开发过程中可能需要打开root的远程访问权限。下面是基本的步骤:
(1)输入mysql -u root -p 然后在输入密码即可进入mysql,然后再输入如下命令:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'ABCDabcd1234.?' WITH GRANT OPTION;mysql> flush privileges;
其中"*.*"代表所有资源所有权限, “'root'@%”其中root代表账户名,%代表所有的访问地址,也可以使用一个唯一的地址进行替换,只有一个地址能够访问。如果是某个网段的可以使用地址与%结合的方式,如10.0.42.%。IDENTIFIED BY 'root',这个ABCDabcd1234.?是指访问密码。WITH GRANT OPTION允许级联授权。
flush privileges;表示刷新访问权限表
(2)使用navicat远程连接