1. 安装YUM Repo
由于CentOS 的yum源中没有mysql,需要到mysql的官网下载yum repo配置文件。
wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
相关报错
下载MySQL,wget不纯在
解决办法:下载wget
代码:yum -y install wget
2. 然后进行repo的安装:
rpm -ivh mysql57-community-release-el7-9.noarch.rpm
执行完成后会在/etc/yum.repos.d/目录下生成两个repo文件mysql-community.repo mysql-community-source.repo
报错
报错信息:可能是系统自动升级正在运行,所以yum被锁定了
解决办法:rm -rf /var/run/yum
来强行解除锁定,然后你的yum就可以运行了
重新进行安装
3. 安装MySQL
使用yum命令即可完成安装
yum install mysql-server
启动msyql:
systemctl start mysqld #启动MySQL
配置MySQL
获取安装时的临时密码:
grep 'temporary password' /var/log/mysqld.log
倘若没有:
1.删除原来安装过的mysql残留的数据
rm -rf /var/lib/mysql
2.再启动mysql
systemctl start mysqld #启动MySQL
登录:
mysql -u root -p
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2019-09-27T17:12:24.846545Z 1 [Note] A temporary password is generated for root@localhost: zgDqW(kl6a0=
( 密码:zgDqW(kl6a0= )
[root@localhost ~]# mysql -u root -p
Enter password: (mysql密码不会显示出来)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.27
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ^C
mysql>
登录成功后修改密码:
set password=password("yourpassword");
密码必须包含大小写、数字、特殊符号
mysql> set password=password("密码");
Query OK, 0 rows affected, 1 warning (0.14 sec)
4. 其他配置
开启远程控制
MySQL默认是没有开启远程控制的,必须添加远程访问的用户
grant all privileges on 数据库名.表名 to 创建的用户名(root)@"%" identified by “密码”;
#数据库名.表名 如果写成*.*代表授权所有的数据库
#如:
grant all privileges on . to root@“113.64.243.1” identified by “123456789”;
- 代码:
grant all privileges on . to root@’%’ identified by “yourpassword”;
- flush privileges; #刷新刚才的内容
mysql> grant all privileges on *.* to root@'%' identified by"密码";
Query OK, 0 rows affected, 1 warning (0.14 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.54 sec)
@ 后面是访问mysql的客户端IP地址(或是 主机名) % 代表任意的客户端,如果填写 localhost 为本地访问(那此用户就不能远程访问该mysql数据库了)。
同时也可以为现有的用户设置是否具有远程访问权限。
5. 其他配置默认编码为utf8:
vi /etc/my.cnf
#添加
[mysqld]
character_set_server=utf8
init_connect=‘SET NAMES utf8’
其他默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid