微服务架构项目搭建过程中的Mysql安装和相关问题
前言
搭建微服务架构的过程中需要使用Mysql数据库,Mysql数据库搭建着实不是一个容易的事情,会碰到各种各样的问题,如果没有一个安装数据库的思路真的很难把数据库安装好,并且掉入到安装的坑当中而无法自拔,因为数据库版本与微服务架构中初始化数据的数据库版本不匹配导致反复安装折腾了一个周,所以把安装思路和遇到的问题跟大家分享一下
数据库的安装
数据库安装前准备
数据库安装时一定要确认的问题就是数据库安装的版本,在工作中一般这一块都会去确认,安装文档应该也会有说明,如果没有说明,记得一定要去确认一下,因为涉及到安装完成后的数据库初始化操作,一般数据库初始脚本与数据库都会进行匹配,本文以下载Mysql5.7为例
<span name=2_2>数据库安装思路</span id>
Linux依赖环境
数据库安装前为了避免出现各种奇葩的问题,先进行依赖的安装最好都安装完成
yum install libaio libaio-devel numactl-libs wget vim -y
yum 这里简单说一下yum。在Linux的学习或者使用过程中应该对yum都有一定的了解
yum是软件仓库,repo文件是Linux中yum的配置文件,定义了软件仓库的细节例如从哪里下载软件包等内容
Exp:在进行安装Mysql报错Cannot find a valid baseurl for repo:base就是由于repo源不对导致
下载安装包并且解压
####### 下载安装包 function1:使用wget命令从官网下载
wget http://dev.mysql.com/get/mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
function2:从官网下载上传至服务器
安装
rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
启动
systemctl start mysqld.service
数据库安装过程常见的问题
不管什么命令都会报错
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
step1:
mysql> alter user 'root'@'localhost' identified by '123456';
#根据提示进行密码修改时报错
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
step2:
mysql> set global validate_password_policy=0;
#根据提示进行策略设置又会报错
ERROR 1193 (HY000): Unknown system variable 'validate_password_policy'
step3:
vi /etc/my.cnf
[mysqld] ## 在mysqld文本段里添加这两行
#添加密码验证插件
plugin-load-add=validate_password.so
#服务器在启动时加载插件,并防止在服务器运行时删除插件
validate-password=FORCE_PLUS_PERMANENT
step4:
[root@localhost]:systemctl restart mysqld
[root@localhost]:mysql -u root -puxndm!@#43
#对密码不进行强校验
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
#密码长度大于1即可
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
step5:
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
最大连接数报错
ERROR 1203 (42000): User dba already has more than 'max_user_connections' active connections。
1.vim /etc/my.cnf
//在[mysqld]下添加
max_connections=10000
2.重启mysql服务
[root@localhost ]:systemctl restart mysqld.service
3.查询最大连接数
mysql> show variables like "max_connections";
输入用户名和密码登录报错权限不足
输入正确的用户名和密码登录报错: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
1.停止mysql数据库
systemctl restart mysqld.service
(或直接 kill -9 [PID] 杀进程!)
2.执行如下命令
mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
3.使用root登录mysql数据库
mysql -u root mysql
4.更新root密码
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
5.刷新权限
mysql> FLUSH PRIVILEGES;
6.退出mysql
mysql> quit
7.重启mysql
[root@localhost ]:systemctl restart mysqld.service
8.使用root用户重新登录mysql
mysql -uroot -p
Enter password: <输入新设的密码newpassword>
数据库客户端连接常见的问题
客户端输入设置的数据库用户名和密码无法登录
报错: null, message from server: "Host '117.89.209.18' is not allowed to connect to this MySQL server" 这是因为你的帐号不允许从远程登陆,只能在服务器所在的机器才可以。这个时候只要在localhost的那台电脑改为%,任何机器都可以访问都可以即可
mysql>use mysql;
mysql>update user set host = '%' where user = 'root'; #远程登录
mysql>select host, user from user;
如果忘记密码在Navicat中如何找回
这种情况不多见,一般是操作过多没记住,但是在navicat有保存就可以找到
1.导出连接
a.选择想要获取密码的数据库即可
b.拿到保存到本地的connections.ncx文件中的Password
2.解密password
a.登陆https://tool.lu/coderunner,使用PHP在线运行工具
粘贴如下代码
<?php
class NavicatPassword
{
protected $version = 0;
protected $aesKey = 'libcckeylibcckey';
protected $aesIv = 'libcciv libcciv ';
protected $blowString = '3DC5CA39';
protected $blowKey = null;
protected $blowIv = null;
public function __construct($version = 12)
{
$this->version = $version;
$this->blowKey = sha1('3DC5CA39', true);
$this->blowIv = hex2bin('d9c7c3c8870d64bd');
}
public function encrypt($string)
{
$result = FALSE;
switch ($this->version) {
case 11:
$result = $this->encryptEleven($string);
break;
case 12:
$result = $this->encryptTwelve($string);
break;
default:
break;
}
return $result;
}
protected function encryptEleven($string)
{
$round = intval(floor(strlen($string) / 8));
$leftLength = strlen($string) % 8;
$result = '';
$currentVector = $this->blowIv;
for ($i = 0; $i < $round; $i++) {
$temp = $this->encryptBlock($this->xorBytes(substr($string, 8 * $i, 8), $currentVector));
$currentVector = $this->xorBytes($currentVector, $temp);
$result .= $temp;
}
if ($leftLength) {
$currentVector = $this->encryptBlock($currentVector);
$result .= $this->xorBytes(substr($string, 8 * $i, $leftLength), $currentVector);
}
return strtoupper(bin2hex($result));
}
protected function encryptBlock($block)
{
return openssl_encrypt($block, 'BF-ECB', $this->blowKey, OPENSSL_RAW_DATA|OPENSSL_NO_PADDING);
}
protected function decryptBlock($block)
{
return openssl_decrypt($block, 'BF-ECB', $this->blowKey, OPENSSL_RAW_DATA|OPENSSL_NO_PADDING);
}
protected function xorBytes($str1, $str2)
{
$result = '';
for ($i = 0; $i < strlen($str1); $i++) {
$result .= chr(ord($str1[$i]) ^ ord($str2[$i]));
}
return $result;
}
protected function encryptTwelve($string)
{
$result = openssl_encrypt($string, 'AES-128-CBC', $this->aesKey, OPENSSL_RAW_DATA, $this->aesIv);
return strtoupper(bin2hex($result));
}
public function decrypt($string)
{
$result = FALSE;
switch ($this->version) {
case 11:
$result = $this->decryptEleven($string);
break;
case 12:
$result = $this->decryptTwelve($string);
break;
default:
break;
}
return $result;
}
protected function decryptEleven($upperString)
{
$string = hex2bin(strtolower($upperString));
$round = intval(floor(strlen($string) / 8));
$leftLength = strlen($string) % 8;
$result = '';
$currentVector = $this->blowIv;
for ($i = 0; $i < $round; $i++) {
$encryptedBlock = substr($string, 8 * $i, 8);
$temp = $this->xorBytes($this->decryptBlock($encryptedBlock), $currentVector);
$currentVector = $this->xorBytes($currentVector, $encryptedBlock);
$result .= $temp;
}
if ($leftLength) {
$currentVector = $this->encryptBlock($currentVector);
$result .= $this->xorBytes(substr($string, 8 * $i, $leftLength), $currentVector);
}
return $result;
}
protected function decryptTwelve($upperString)
{
$string = hex2bin(strtolower($upperString));
return openssl_decrypt($string, 'AES-128-CBC', $this->aesKey, OPENSSL_RAW_DATA, $this->aesIv);
}
};
//需要指定版本两种,11或12
//$navicatPassword = new NavicatPassword(11);
$navicatPassword = new NavicatPassword(11);
//解密
//$decode = $navicatPassword->decrypt('15057D7BA390');
$decode = $navicatPassword->decrypt('15057D7BA390');
echo $decode."\n";
?>
用文件中的文件中的password,替换上面代码中的$decode = $navicatPassword->decrypt('E75BF077AB8BAA3AC2D5');
点击运行之后,就会得到真实密码
本文来自博客园,作者:天行者_sky,转载请注明原文链接:https://www.cnblogs.com/Skywal/p/15880237.html