安装MySQL8数据库
1、安装前准备
1.1、关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
1-2、编辑 /etc/selinux/config 关闭 SELinux
SELINUX=disabled
1-3、创建MySQL用户和组
# 创建mysql的home目录
mkdir -p /home/mysql
# 创建mysql组
groupadd mysql
# 创建mysql用户 并指定组合默认路径
useradd -r -d /home/mysql -g mysql mysql
# 将mysql默认路径的用户和组改成mysql
chown -R mysql:mysql /home/mysql
2、安装MYSQL
下载mysql通用版mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
2-1、将包复制到 /usr/local 目录下
2-2、解压MySQL安装包
cd /usr/local/
tar -xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
2-3、解压后的mysql目录进行改名
mv mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz mysql
2-4、设置目录 /usr/local/mysql 的所有者为mysql
chown -R mysql:mysql /usr/local/mysql
2-5、查看MYSQL目录结构
tree -d -L 1 mysql
2-6、编辑文件 /etc/profile 为mysql配置环境 在文件的最后增加:
export PATH=$PATH:/usr/local/mysql/bin
2-7、生效环境配置
source /etc/profile
2-8、创建MYSQL数据目录
# 创建数据目录
mkdir /usr/local/mysql/data
# 将数据目录的用户和组改成mysql
chown mysql:mysql /usr/local/mysql/data
# 更改数据目录权限
chmod 750 /usr/local/mysql/data
2-9、新建MySQL配置文件 /etc/my.cnf 并添加以下内容:
[mysqld]
server-id=1
prot=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/data/error.log
socket=/tmp/mysql.sock
pid-file=/usr/local/mysql/data/mysql.pid
character-set-server=utf8
lower_case_table_names=1
innodb_log_file_size=1G
default-storage-engine=INNODB
default_authentication_plugin=mysql_native_password
[client]
port=3306
default-character-set=utf8
2-10、初始化mysql数据库
mysqld --initialize --user msyql
2-11、查看初始化的输出日志 确定mysql的root用户密码
cat /usr/local/mysql/data/error.log
输出信息如下:
[Server] /usr/local/mysql-8.0.20-linux-glibc2.12-x86_64/bin/mysqld
(mysql 8.0.20) initializing of server in
...
看到如下信息,表示初始化成功:
[InnoDB] InnoDB initialization has started.
[InnoDB] InnoDB initialization has ended.
3、启动与关闭MySQL数据库
3-1、启动mysql数据库
cd /usr/local/mysql
support-files/mysql.server start
输出信息:
Starting MySQL...Success!
3-2、查看Mysql数据库的状态
supoort-files/mysql.server status
3-3、配置mysql数据库的开机自启服务
# 复制mysql.server 到/etc/init.d/目录下
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#使用chkconfig 添加mysql服务到开机启动的列表中
chkconfig --add mysqld
使用systemctl命令管理mysql服务
systemctl status mysqld
3-4、查看系统配置的开机自启列表
chkconfig --list
mysqld 0:off1:off2:on3:on4:on5:on6:off
3-5、关闭mysql数据库
cd /usr/local/mysql
support-files/mysql.server stop
关闭mysql数据库也可以使用mysqladmin命令
mysqladmin -uroot -pWelcome_1 shtudown
另外一种关闭方法:
#mysql -uroot -pWelcome_1
mysql>shutdown
MySQL连接
1、本地链接
临时密码 /usr/local/mysql/data/error.log
mysql -uroot -p
修改密码:
mysql>alter user'root'@'localhost' identified by 'Welcome_1';
允许用户root进行远程登录
mysql>create user 'root'@'%' identified by 'Welcome_1';
mysql>grant all on *.* to 'root'@'%';
mysql>flush privileges;
2、远程链接
创建用户 "user001" 密码 Welcome_1
mysql>create user'user001'@'%' identified by 'Welcome_1';
为用户授权
grant all on mysql.* to 'user001'@'%';
flush privileges;
使用rooot用户查看系统的user表
use mysql
select host,user from user;
使用root用户查看系统的db表
use mysql
select host,user,db from db where user='user001';