1.linux系统centos安装mysql
1.1虚拟机的安装
vmware创建环境的方式采用默认
centos安装的时候选择具有图形化界面的方式,带gui服务器下面的都行
虚拟机保证能够进行上网
1.2 mysql的安装
在虚拟机中启动终端,切换到root权限,安装时root的密码为123456
su
yum install wget
wget http://repo.mysql.com/mysql57-community-release-el7.rpm
cat /etc/centos-release
rpm -ivh mysql57-community-release-el7.rpm
yum install mysql-server
出现这样的错误
mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm 的公钥尚未安装
失败的软件包是:mysql-community-libs-compat-5.7.41-1.el7.x86_64
GPG 密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
解决方案为
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
之后再次安装mysql服务
yum install mysql-server
启动mysql服务端mysqld后,因为没有配置root及密码,启动mysql客户端会出错
systemctl restart mysqld
mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
参考的网页:
1.3 mysql的启动
采用这样的方式安装mysql时,无论使用的下列哪种方式都会出错
mysql
mysql -u root -p
解决方式,需要vim打开配置文件,写入跳过密码检查
1. vi /etc/my.cnf
2. \#添加跳过密码验证
3. skip-grant-tables
按i进入插入模式,编辑配置文档,esc退出插入模式,输入下列进行强制写入
:wq!
重启mysql服务
systemctl restart mysqld
终端输入mysql进行数据库,切换到mysql,将密码置空
mysql
mysql> use mysql
mysql> update user set authentication_string='' where user='root';
刷新权限
flush privileges;
设置加密规则并更新新密码,授权(直接复制这些SQL语句你的密码会更新为123456)
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; #密码不过期
alter user 'root'@'localhost' identified by '123456';
grant all privileges on *.* to "root"@'localhost';
flush privileges;
quit;
设置成功后,重启mysql服务,使用新密码登录
systemctl restart mysqld
mysql -u root -p
原文链接:
2.finnalshell连接centos7和虚拟机中mysql
2.1 虚拟机连接(端口22)
在虚拟机中使用ifconfig获取虚拟机中的ip地址,inet后面的192.168.180.134即为IP地址
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.180.134 netmask 255.255.255.0 broadcast 192.168.180.255
inet6 fe80::32c8:8c4:6aca:9e46 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:01:f8:30 txqueuelen 1000 (Ethernet)
RX packets 643823 bytes 934121811 (890.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 141080 bytes 8640779 (8.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
在finalshell中添加新的连接
名称任意
主机名称为虚拟机的ip
root用户和相关的密码(123456)
2.2 虚拟机中的mysql连接(使用Navicat3306端口)
由于mysql中的root默认只能是本地主机进行访问的,如果我们想要能够使用数据库管理工具进行远程连接,
默认的root用户只能当前节点localhost访问,是无法远程访问的,我们还需要创建一个root账户,用户远程访问
create user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
root用户分配权限
grant all on *.* to 'root'@'%';
端口22被finnalshell 所使用,不能在Navicat使用ssh连接,使用3306端口
ping 192.168.180.134
telnet 192.168.180.134 3306
在虚拟机中终端,查看防火墙的状态
systemctl status firewalld
永久打开防火墙3306端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
重新载入
firewall-cmd --reload
查看(返回yes端口打开)
firewall-cmd --zone=public --query-port=3306/tcp
删除
firewall-cmd --zone=public --remove-port=3306/tcp --permanent
数据库当然一般都为开机即自动运行的,这样就让我们不需要每次开机都要手动重新启动了,
CentOS7执行:
systemctl enable mysqld
启动mysql服务
systemctl start mysqld.service
停止mysql服务
systemctl stop mysqld.service
重启mysql服务
systemctl restart mysqld.service
查看mysql服务当前状态
systemctl status mysqld.service
设置mysql服务开机自启动
systemctl enable mysqld.service
停止mysql服务开机自启动
systemctl disable mysqld.service