一、yum安装
新版本(亲测多次使用,没问题)
下载mysql的yum仓库文件
[root@localhost /]#wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
安装mysql源
[root@localhost /]yum -y install mysql mysql57-community-release-el7-11.noarch.rpm
安装Mysql
yum -y install mysql-community-server
最后一行出现报错:mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm 的公钥尚未安装
所以要导入公钥(如果是mysql-community-release-el7-5.noarch.rpm版本,就不需要这一步)
[root@localhost /]rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
再次安装
[root@localhost /]yum -y install mysql-community-server
二、开启mysql服务
启动mysql
[root@localhost /]systemctl start mysqld.service
设置mysql自启动
[root@localhost /]systemctl enable mysqld.service
mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 四 2019-12-12 17:00:00 CST; 20s ago
查看MySQL运行状态:
[root@localhost /]status mysqld.service
只要出现 active(running) ,就表示正在运行
或者这个也可以
[root@localhost /]ps aux|grep mysql
三、配置
注意要开启主机电脑mysql服务!
端口放行
为了保证外网能访问mysql,能使用navicat连接云服务器,需要设置防火墙,确保防火墙放行mysql的端口号
开启防火墙:
[root@localhost /]systemctl start firewalld
添加防火墙放行端口号:mysql端口,默认3306
[root@localhost /]firewall-cmd --zone=public --add-port=3306/tcp --permanent
查看端口开放情况:
[root@localhost /]firewall-cmd --list-ports
如果是阿里云云服务器,还需要在控制台安全组添加出入口放行规则
查看修改配置
查看mysql位置:
[root@localhost /]which mysqld
使用上面命令查到的目录,查看mysql使用的配置文件默认路径
[root@localhost /]/usr/sbin/mysqld --verbose --help |grep -A 1 'Default options'
编辑一些配置文件 /etc/my.cnf
1、自定义端口:加 port=端口号
2、远程连接数据库运行报错:
》》要在配置文件里面加
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
3、character-set-server=utf8mb4
。。。
四、密码设置
①通过命令在日志文件中找出密码:
查找初始密码
[root@localhost /]# grep "password" /var/log/mysqld.log
这里密码就是7IB<p#rpwC(u
登录
[root@localhost /]# mysql -uroot -p
回车输入上述密码,回车确定
进入mysql,开始修改密码
** 注意每条命令后都要加分号 **
mysql>ALTER USER 'root'@'localhost' IDENTIFIED BY '1AB<p#rpwC(y';
或者
set password for root@localhost = password('1AB<p#rpwC(y')
这里设置用户名为root的密码,密码为1AB<p#rpwC(y
需要设置复杂密码,不想设置复杂密码,可以更改安全级别
刷新配置,保证立即生效
mysql>flush privileges;
②免密设置密码 (此版本下待测):
1、编辑 my.cnf文件
[root@localhost ~]# vim /etc/my.cnf
在[mysqld]的
…
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
下添加 skip-grant-tables
:wq保存退出
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/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
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
...
重启mysql服务,使配置生效
[root@localhost ~]# systemctl restart mysqld
2、无密码登录mysql(输入用户名直接按回车)
[root@localhost ~]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.46 MySQL Community Server (GPL)
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.
3、通过更改密码设置密码
mysql> update mysql.user set password=password(‘想要设置的密码’) where user=‘用户名’;
mysql> update mysql.user set password=password('rootp') where user='root';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
“不重启MySQL服务”的情况下直接生效
将当前user和privilige表中的用户信息、权限设置从MySQL数据库的内置库中提取到内存里。
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
退出
mysql> quit;
Bye
4、修改my.cnf,恢复配置
[root@localhost ~]# vim /etc/my.cnf
删除之前在[mysqld]里插入的skip-grant-tables
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/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
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
...
重启mysql服务,配置生效
[root@localhost /]# systemctl restart mysqld
5、登录验证
[root@localhost ~]# mysql -u root -p
回车后输入刚才配置的密码 rootp