查看操作系统版本
[root@node03 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
查看系统内核版本
[root@node03 ~]# uname -a
Linux node03 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
https://dev.mysql.com/downloads/mysql/
[root@node03 soft]# rpm -qa | grep -i mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
[root@node03 soft]# rpm -e mariadb-libs-5.5.44-2.el7.centos.x86_64 --nodeps
[root@node03 soft]# rpm -qa | grep -i mariadb
[root@node03 soft]#
[root@node03 soft]# tar -xvf mysql-5.7.30-1.el7.x86_64.rpm-bundle.tar
其中最简单的安装mysql-server服务,只需要安装如下4个软件包即可,我们可以使用rpm -ivh命令进行安装
mysql-community-common-5.7.30-1.el7.x86_64.rpm
mysql-community-libs-5.7.30-1.el7.x86_64.rpm --(依赖于common)
mysql-community-client-5.7.30-1.el7.x86_64.rpm --(依赖于libs)
mysql-community-server-5.7.30-1.el7.x86_64.rpm --(依赖于client、common)
依次安装顺序为common→libs→client→server
[root@node03 mysql]# rpm -ivh mysql-community-common-5.7.30-1.el7.x86_64.rpm
[root@node03 mysql]# rpm -ivh mysql-community-libs-5.7.30-1.el7.x86_64.rpm
[root@node03 mysql]# rpm -ivh mysql-community-client-5.7.30-1.el7.x86_64.rpm
[root@node03 mysql]# rpm -ivh mysql-community-server-5.7.30-1.el7.x86_64.rpm
注:ivh中, i-install安装;v-verbose进度条;h-hash哈希校验
// 指定datadir, 执行后会生成~/.mysql_secret密码文件(5.7以后不在使用)
[root@node03 mysql]# mysql_install_db --datadir=/var/lib/mysql
// 初始化,执行后会在/var/log/mysqld.log生成随机密码
[root@node03 mysql]# mysqld --initialize
[root@node03 mysql]# chown mysql:mysql /var/lib/mysql -R
使用命令 service mysqld start 或 systemctl start mysqld.service 启动MySQL服务。(如果mysql服务无法启动,就重启一下系统)
[root@node03 mysql]# systemctl start mysqld.service 启动mysql
systemctl start mysqld.service 启动mysql
systemctl status mysqld.service 查看mysql状态
systemctl stop mysqld.service 关闭mysql
查看mysql进程 ps -ef|grep mysql
查看3306端口 netstat -anop|grep 3306
由于MySQL5.7.4之前的版本中默认是没有密码的,登录后直接回车就可以进入数据库,进而进行设置密码等操作。其后版本对密码等安全相关操作进行了一些改变,在安装过程中,会在安装日志中生成一个临时密码。
查询MySQL生成的随机密码:
[root@node03 mysql]# grep 'temporary password' /var/log/mysqld.log # 在/var/log/mysqld.log文件中搜索字段‘temporary password’
2018-07-18T06:02:23.579753Z 1 [Note] A temporary password is generated for root@localhost: n(jPp4l-C33#
登录并设置密码:
[root@node03 mysql]# mysql -u root -p
Enter password: # 在这里输入密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30
Copyright (c) 2000, 2018, 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> set password=password('密码'); #设置密码
create user 用户名@IP地址 identified by '密码';
>用户只能在指定的IP地址上登录。
create user 用户名@'%' identified by '密码';
>用户可以在任意IP地址上登录。
grant 权限1,...,权限n on 数据库.* 用户名@IP地址
> * 是指数据库的表,函数,存储过程,触发器等东西。
> 权限、数据库、用户。
> 给用户分派在指定的数据库上的指定的权限。
> 例如: grant create, alter, drop, insert, update, delete, select on mydb1.* to user1@localhost;
给user1用户分派在mydb1数据库上的create, alter, drop, insert, update, delete, select权限。
grant all on 数据库.* to 用户名@IP地址;
> 给用户分派指定数据库上的所有权限。
mysql> CREATE USER 'test'@'%' IDENTIFIED BY '123456a?';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'test'@'%';
mysql> flush privileges;
# 授予root用户远程访问权限
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456a?' mysql> with grant option;
flush privileges;
# 检查是否已经是开机启动
systemctl list-unit-files | grep mysqld
# 设置开机启动
systemctl enable mysqld.service
配置文件:/etc/my.cnf
日志文件:/var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
配置默认编码为utf8
修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
最后需要特别提醒注意的一点是,新版的mysql数据库下的user表中已经没有Password字段了, 而是将加密后的用户密码存储于authentication_string字段
[root@i3467544tdsxfrZ ~]# rpm -ivh mysql-community-server-5.7.30-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.30-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
libaio.so.1()(64bit) is needed by mysql-community-server-5.7.30-1.el7.x86_64.rpm
libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-5.7.30-1.el7.x86_64.rpm
libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-5.7.30-1.el7.x86_64.rpm
真正原因是MySQL依赖libaio,所以先要安装libaio,我们通过 rpm -qa|grep libaio 命令查看一下,如下:
能正常安装MySQL
[root@node03 mysql]# rpm -qa | grep -i libaio
libaio-0.3.109-13.el7.x86_64
不能正常安装MySQL
[root@node03 mysql]# rpm -qa | grep -i libaio
[root@node03 mysql]#
libaio包下载地址(根据自己linux版本需要选择libaio下载):http://mirror.centos.org/centos/7/os/x86_64/Packages/libaio-0.3.109-13.el7.x86_64.rpm
安装libaio(若在有网情况下可执行yum install libaio)
[root@i3467544tdsxfrZ ~]# rpm -ivh libaio-0.3.109-13.el7.x86_64.rpm
然后再安装 mysql-community-server-5.7.30-1.el7.x86_64.rpm ,就可以正常安装了
[root@i3467544tdsxfrZ ~]# rpm -ivh mysql-community-server-5.7.30-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.30-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-server-5.7.30-1.el7################################# [100%]