Mysql 安装
一、配置hosts文件
hostname 查看主机名
hostname -i:查看本机对应的IP
修改主机名
vi /etc/sysconfig/network
输入以下内容
NETWORKING=YES
HOSTNAME=hjy02
修改hosts配置
vi /etc/hosts
输入以下内容
192.168.120.135 hjy02
查看文件内容
cat /etc/hosts
ping 主机名
ping hjy02
修改完成后重启
reboot
二、卸载主机自带的mysql
rpm -qa | grep mysql
rpm -e --nodeps *mysql*
rpm -qa | grep mariadb
rpm -e --nodeps mariadb*
三、安装mysql 8.0
cd /mysql/
tar -xvf mysql-8.0.19-linux-glibc2.12-x86_64.tar
rm -f mysql-8.0.19-linux-glibc2.12-x86_64.tar
xz -d mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
tar -xvf mysql-8.0.19-linux-glibc2.12-x86_64.tar
mv mysql-8.0.19-linux-glibc2.12-x86_64 /usr/local/
mv /usr/local/mysql-8.0.19-linux-glibc2.12-x86_64 /usr/local/mysql
cd /usr/local/mysql
mkdir /var/lib/mysql
mkdir -p /usr/local/mysql/log
mkdir /usr/local/mysql/data
四、创建mysql用户和组,目录和授权
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /var/lib/mysql
五、配置mysql环境变量
vi ~/.bash_profile
PATH=$PATH:/usr/local/mysql/bin:$HOME/bin
退出登录
exit
查看mysql环境变量是否配置成功
which mysql
输出以下内容则表示mysql环境变量设置成功
/usr/local/mysql/bin/mysql
六、配置参数文件
编辑 my.cnf
文件
vi /etc/my.cnf
Code
1 |
|
七、mysql 数据库初始化
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
切换到log目录
cd /usr/local/mysql/log
查看日志记录
tail -100f hjy02-error.err
拷贝文件中的临时密码,以备登录时使用:
2020-04-07T16:20:16.625578Z 5 [Note] [MY-010454] [Server]
A temporary password is generated for root@localhost: hoh+pk*Gp5rp
切换到data目录
cd /usr/local/mysql/data
查看文件夹内容
ll
如下图,说明数据库初始化成功
八、配置Centos Linux启动和停止脚本
编辑mysqld.service
文件
vi /usr/lib/systemd/system/mysqld.service
Code
1 |
|
修改文件权限
chmod u+x /usr/lib/systemd/system/mysqld.service
九、配置systemctl 启动和停止方法
#重新加载配置
systemctl daemon-reload
#停止服务
systemctl stop mysqld
#启动服务
systemctl start mysqld
#设置开机自动启动
systemctl enable mysqld
#查看运行状态
systemctl status mysqld
十、手工启动和停止Mysql方法
nohup /usr/local/mysql/bin/mysql_safe --defaults-file=/etc/my.cnf&
mysqladmin -uroot -p shutdown -S /var/lib/mysql/mysql.sock
查看mysql进程
ps -ef | grep mysql
十一、修改root密码
mysql -u root -p
use mysql;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'rootroot';
exit;
十二、允许root远程登录
mysql -u root -p
use mysql;
select host,user from user where user='root';
create USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
grant all privileges on *.* to 'root'@'%' with grant option;
exit;
测试是否可以远程登录
mysql -u root -p -h 192.168.120.135
#此处远程登录的密码为:`root`
十三、远程连接需关闭防火墙
#查看防火墙状态
firewall-cmd --state
systemctl status firewalld.service
#停止防火墙服务
systemctl stop firewalld.service
#禁用防火墙
systemctl disable firewalld.service
十四、数据库操作与配置
1.创建数据库
create database itpuxdb DEFAULT CHARSET utf8mb4;
2.创建用户
CREATE USER 'itpux'@'%' IDENTIFIED BY 'itpux';
3.查看所有用户
select host,user from mysql.user;
4.为itpux用户分配权限
grant all privileges on itpuxdb.* to 'itpux'@'%' with grant option;
5.创建表
Code
1 |
|
6.插入数据
insert into itpux01 values (1,'张三');
insert into itpux01 values (2,'李四');
insert into itpux01 values (3,'王五');
insert into itpux01 values (4,'赵六');
7.查询数据
select * from itpuxdb.itpux01;
use itpuxdb
select * from itpux01;
如下图:
十五、卸载
rm -rf /usr/local/mysql
rm -rf /etc/my.cnf
rm -rf /var/lib/mysql
shutdown -h now
关机还原,完成卸载。