Linux服务器配置之MySQL安装
建议使用yum的方式,安装包的方式会有好多奇奇怪怪的问题。
MySQL安装(yum方式安装)
- 检查系统本身是否有预装的mysql:
rpm -qa | grep mysql #检查是否安装了mysql
rpm -qa | grep mariadb #检查是否安装了mariadb
rpm -e xxx #一般使用此命令即可卸载成功
rpm -e --nodeps xxx #卸载不成功时使用此命令强制卸载)
- 安装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
- 启动MySQL:
执行命令systemctl start mysqld.service来启动mysql服务,systemctl status mysqld.service可查看mysql服务运行状态,也可以lsof -i:3306通过端口号查看。
tar.gz安装包方式安装
地址: mysql下载地址.
- 上传:先将mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz包上传
- 解压:
tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz - 移动并修改名称:
mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql - 修改权限:
cd /usr/local/mysql
chown -R mysql:mysql ./
- 安装:
./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设置
- 服务启动(
service mysqld start)后会生成初始密码,通过grep "password" /var/log/mysqld.log查看 - 登录MySQL:通过
mysql -uroot -p登录会看到如下,只需要在Enter password后复制上刚才查到的初始密码就可以了。
[root@instance-3oclcot9 local]# mysql -uroot -p
Enter password:
- 修改Mysql的root用户密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的密码';
- 如果设置的密码太简单会有如下报错,这里密码遵守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 ;
- 设置远程登录:
//进入mysql数据库后
use mysql;
//这里的%是允许所有的ip地址登录这个服务器的mysql服务器
update user set host='%' where user='root' and host='localhost';
//刷新权限
flush privileges;
//查看防火墙的状态
systemctl status firewalld
- 设置服务自启动:
方法一:
#自启动
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
本文详细介绍在Linux服务器上使用yum及tar.gz两种方式安装MySQL的过程,并提供了my.cnf配置文件的内容,涵盖服务启动、密码修改、远程登录设置及数据导入等关键步骤。
1486

被折叠的 条评论
为什么被折叠?



