CentOS7之MySQL安装

本文详细介绍在Linux服务器上使用yum及tar.gz两种方式安装MySQL的过程,并提供了my.cnf配置文件的内容,涵盖服务启动、密码修改、远程登录设置及数据导入等关键步骤。

建议使用yum的方式,安装包的方式会有好多奇奇怪怪的问题。

MySQL安装(yum方式安装)

  1. 检查系统本身是否有预装的mysql
rpm -qa | grep mysql #检查是否安装了mysql
rpm -qa | grep mariadb #检查是否安装了mariadb
rpm -e xxx  #一般使用此命令即可卸载成功
rpm -e --nodeps xxx #卸载不成功时使用此命令强制卸载)
  1. 安装MySql
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
//下载mysql的rpm的包
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
  1. 启动MySQL
    执行命令systemctl start mysqld.service来启动mysql服务,systemctl status mysqld.service可查看mysql服务运行状态,也可以lsof -i:3306通过端口号查看。

tar.gz安装包方式安装

地址: mysql下载地址.

  1. 上传:先将mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz包上传
  2. 解压tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
  3. 移动并修改名称mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql
  4. 修改权限
cd /usr/local/mysql
chown -R mysql:mysql ./
  1. 安装
./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize

最后一行是 默认密码
在这里插入图片描述
6. 将mysql进程放入系统进程中,命令如下

cp support-files/mysql.server /etc/init.d/mysqld

MySQL设置

  1. 服务启动(service mysqld start)后会生成初始密码,通过grep "password" /var/log/mysqld.log 查看
  2. 登录MySQL:通过mysql -uroot -p登录会看到如下,只需要在Enter password后复制上刚才查到的初始密码就可以了。
[root@instance-3oclcot9 local]# mysql -uroot -p
Enter password:
  1. 修改Mysql的root用户密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的密码';
  1. 如果设置的密码太简单会有如下报错,这里密码遵守mysql的安全策略(大写英文,小写英文,数字,特殊字符)。
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

如果不想设置复杂密码可以使用下面这个
mysql5.7版本的密码列名变为authentication_string

#选择库
SET PASSWORD = PASSWORD('123456');
use mysql;
update user set authentication_string = password('新密码'),password_last_changed=now() where user='root';
#再执行刷新
flush privileges ; 
  1. 设置远程登录
//进入mysql数据库后
use mysql;
//这里的%是允许所有的ip地址登录这个服务器的mysql服务器
update user set host='%' where user='root' and host='localhost';
//刷新权限
flush privileges;
//查看防火墙的状态
systemctl status firewalld
  1. 设置服务自启动

方法一:

#自启动
systemctl enable mysqld
#关闭自启动
systemctl disable mysqld
#看设置后的状态
systemctl list-unit-files | grep mysql

方法二:

vim /etc/rc.local
添加service mysqld start(这个方法我没使用,但看别人的文章有就加进来了)

注意如果和我一样做了后面my.cnf的更改要看看basedir和datadir的路径存在么,不存在就mkdir创建,不然会启动失败的。

数据导入

对应数据库下执行source /路径/xxx.sql

my.cnf配置内容

#NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
port = 3306
default-character-set=utf8

[mysqld]
# 一般配置选项
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character-set-server=utf8
default_storage_engine = InnoDB

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值