- 查看是否已经安装了MySQL,命令:
yum list installed|grep mysql
或rpm -qa|grep -i mysql
- 查看yum库mysql版本信息(电脑需正常联网),命令:
yum list|grep mysql
或yum -y list mysql*
- yum命令安装mysql及其服务,命令
yum -y install mysql-server mysql mysql-devel
- 查看安装结果,命令:
rpm -qi mysql-server
- 解决mysql-service,没有yum命令下,无可用包的问题(CentOS 7 版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了)
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
- 选择系统,查看mysql数据
- 查看系统版本&位数
cat /etc/redhat-release
&getconf LONG_BIT
- 选择mysql下载
- 将下载好的文件ftp到服务器上并解包,参考https://www.jianshu.com/p/27bec7340f61
- rpm命令安装MySQl的服务端,客户端,开发库这三个包,min命令:
rpm -ivh mysql-community-server-5.7.20-1.el7.x86_64.rpm --nodeps
rpm -ivh mysql-community-client-5.7.20-1.el7.x86_64.rpm --nodeps
rpm -ivh mysql-community-devel-5.7.20-1.el7.x86_64.rpm --nodeps
rpm -ivh mysql-community-common-5.7.20-1.el7.x86_64.rpm --nodeps
rpm -ivh mysql-community-libs-5.7.20-1.el7.x86_64.rpm --nodeps
rpm -ivh mysql-community-libs-compat-5.7.20-1.el7.x86_64.rpm --nodeps
- 查看安装结果,命令:
rpm -qa|grep -i mysql
- 启动mysql,命令:
service mysqld start
或systemctl start mysqld
并查看启动结果systemctl status mysqld
;
设置开机启动命令
systemctl enable mysqld
systemctl daemon-reload
- 查看初始密码,文件路径 /var/log/mysqld.log,并记下
- 修改初始密码命令:
mysql -uroot -p
之后输入:ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
或set password for 'root'@'localhost'=password('新密码');
- 退出(exit或quit)并用新密码登录mysql(mysql -u root -p)
- 添加远程登录用户
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,添加一个新的帐户:GRANT ALL PRIVILEGES ON *.* TO 'usrname'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
- 新用户登录
mysql -u usrname -p
- 配置utf-8编码
修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
- 重启mysql,使配置生效
systemctl restart mysqld
- 查看配置结果
show variables like '%character%';
- 常用文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid