1、yum仓库下载MySQL:
sudo yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
2、yum安装MySQL:
sudo yum install mysql-community-server
3、启动MySQL服务:
sudo service mysqld start
4、检查MySQL服务状态:
sudo service mysqld status
5、查看初始密码(如无内容直接跳过):
sudo grep 'temporary password' /var/log/mysqld.log
6、本地MySQL客户端登录:
mysql -u root -p
7、输入密码为第5步查出的,如果没有,直接回车,
然后就报了这个错:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
解决方法:
先按照提示找/var/lib/mysql/mysql.sock发现没这个文件,然后又去找mysql.sock
再输入:
alter user 'root'@'localhost' identified by '123456'; #设置密码为123456
报了这个错误(这个错误是说你的设置的密码太简单了,不安全,你重新设置下密码就可以了)
ERROR 1819 (HY000):Your password does not satisfy the current policy requirements
如果你偏要设置就再输入:
set global validate_password.policy=0;
mysql数据库5.7和8.0由于全局变量名字改了所以运行的命令不同,但是本质一样。
set global validate_password_policy=0;
set global validate_password.policy=0;
再输入:(如果说还显示错误那就把密码设置复杂点,加点字母什么的)
alter user 'root'@'localhost' identified by '123456'; #设置密码为123456
输入quit
退出mysql
再输入:
mysql -u root -p
输入刚刚设置好的密码,就可以成功登录了
打开navicat选择阿里云连接
选择–>高级,把编码改为utf-8,防止乱码
选择–>SSH–>点击–>使用SSH隧道
连接成功
测试成功后会显示
测试成功后点确定就ok了
这时候你就可以直接通过navicat操作服务器上的数据库了。
目的:你上传的项目工程要连接数据库,如果没授权可能会出现数据库不能连接的错误,因为你项目是连接服务器上的数据库,服务器上的数据库没有授权用户IP权限。
出现错误的解决方法:
message from server: "Host is not allowed to connect to this MySQL server
先查看数据库
show databases; use mysql; select host,user from user; +-------------------------+------+ | host | user | +-------------------------+------+ | 127.0.0.1 | root | | iz2ze3hkpeilimy9kn68tvz | | | iz2ze3hkpeilimy9kn68tvz | root | | localhost | | | localhost | root | +-------------------------+------+ 5 rows in set (0.00 sec)
改表法
#直接用更新语句 update user set host = '%' where user = 'root'; select host, user from user; +-------------------------+------+ | host | user | +-------------------------+------+ | % | root | | 127.0.0.1 | root | | iz2ze3hkpeilimy9kn68tvz | | | iz2ze3hkpeilimy9kn68tvz | root | | localhost | | | localhost | root | +-------------------------+------+ 6 rows in set (0.00 sec) #成功后,需要刷新表 FLUSH PRIVILEGES
授权法
#例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。 GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; #刷新表 FLUSH PRIVILEGES #如果出错,那就再执行一次 GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; select host, user from user; +-------------------------+------+ | host | user | +-------------------------+------+ | % | root | | 127.0.0.1 | root | | iz2ze3hkpeilimy9kn68tvz | | | iz2ze3hkpeilimy9kn68tvz | root | | localhost | | | localhost | root | +-------------------------+------+ 6 rows in set (0.00 sec) #刷新表,记得刷新,不然没有效果 FLUSH PRIVILEGES
授权法其他应用
#如果你想允许用户myuser从ip为192.168.1.3的主机连接到mysql服务器,并使用mypassword作为密码 GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; FLUSH PRIVILEGES #如果你想允许用户myuser从ip为192.168.1.3的主机连接到mysql服务器的dk数据库,并使用mypassword作为密码 GRANT ALL PRIVILEGES ON dk.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; FLUSH PRIVILEGES