美文网首页Spring boot
JDBC连接数据库时出现的Public Key Retrieva

JDBC连接数据库时出现的Public Key Retrieva

作者: 三也视界 | 来源:发表于2020-12-10 14:18 被阅读0次

    关于JDBC连接数据库时出现的Public Key Retrieval is not allowed错误

    连接数据库的url中,加上allowPublicKeyRetrieval=true参数,经过验证解决该问题。

    image.png image.png image.png

    mac下MySql启动连接报错:Authentication plugin ‘caching_sha2_password’ cannot be loaded: dlopen(/usr/local/mysql/lib/plugin/caching_sha2_password.so, 2): image not found。

    问题:在连接数据库时不能加载‘caching_sha2_password’这个插件,也就是不能对身份验证。

    解决方案:

    1.打开系统偏好设置,找到mysql,点击Initialize Database.

    2.输入你的新密码,记住这个密码,用于后期链接数据库的登陆使用.

    3.选择‘Use legacy password‘.

    4.重启mysql服务.

    启动MySQL服务

    sudo /usr/local/MySQL/support-files/mysql.server start
    

    停止MySQL服务

    sudo /usr/local/mysql/support-files/mysql.server stop
    

    重启MySQL服务

    sudo /usr/local/mysql/support-files/mysql.server restar
    

    ... ERROR! The server quit without updating PID file (/usr/local/mysql/data/philos.local.pid).

    Mostly this is a permission issue

    Step1: Check the error file first.
    
    tail -f /usr/local/mysql/data/*.err
    Step2: Do a complete shut down or kill the process. Confirm that no mysql process is running
    
    mysqladmin -uroot shutdown
    sudo killall mysqld 
    
    ps -ef | grep mysql
    Step3: Give permisiions
    
    sudo chown -RL root:mysql /usr/local/mysql
    sudo chown -RL mysql:mysql /usr/local/mysql/data
    chmod -R 755 /usr/local/mysql/data
    Step4: Start mysql
    
    sudo mysql.server start
    
    mysql -u root -p
    Enter password: 
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    

    既然现在没法登录到数据库中,改密码和添加用户等操作也无从谈起。好在MySQL中还提供了一种免去密码校验进入数据库的方法,我们就先使用这种方法登入到数据库中。然后将默认密码替换掉,上面的问题就可以解决掉啦~具体操作如下(如果想要快速解决,可以直接看最下面的快速方案)

    一、找到Mysql配置文件
    这个操作,在不同平台下对应的不同的操作文件
    mac系统中配置文件是mysql安装目录support_file下的my-default文件

    MAC下安装Mysql找不到my.cnf配置文件的问题,并自己创建my.cnf文件

    1.在 /etc 新建 my.cnf 文件

    sudo vim my.cnf
    1
    2.将如下配置内容写入到文件中

    Example MySQL config file for medium systems.

    This is for a system with little memory (32M - 64M) where MySQL plays

    an important part, or systems up to 128M where MySQL is used together with

    other programs (such as a web server)

    MySQL programs look for option files in a set of

    locations which depend on the deployment platform.

    You can copy this option file to one of those

    locations. For information about these locations, see:

    http://dev.mysql.com/doc/mysql/en/option-files.html

    In this file, you can use all long options that a program supports.

    If you want to know which options a program supports, run the program

    with the "--help" option.

    The following options will be passed to all MySQL clients

    [client]
    default-character-set=utf8

    password = your_password

    port = 3306
    socket = /tmp/mysql.sock

    Here follows entries for some specific programs

    The MySQL server

    [mysqld]
    character-set-server=utf8
    init_connect='SET NAMES utf8
    port = 3306
    socket = /tmp/mysql.sock
    skip-external-locking
    key_buffer_size = 16M
    max_allowed_packet = 1M
    table_open_cache = 64
    sort_buffer_size = 512K
    net_buffer_length = 8K
    read_buffer_size = 256K
    read_rnd_buffer_size = 512K
    myisam_sort_buffer_size = 8M
    character-set-server=utf8
    init_connect='SET NAMES utf8'

    Don't listen on a TCP/IP port at all. This can be a security enhancement,

    if all processes that need to connect to mysqld run on the same host.

    All interaction with mysqld must be made via Unix sockets or named pipes.

    Note that using this option without enabling named pipes on Windows

    (via the "enable-named-pipe" option) will render mysqld useless!

    skip-networking

    Replication Master Server (default)

    binary logging is required for replication

    log-bin=mysql-bin

    # binary logging format - mixed recommended  
    binlog_format=mixed  
    
      # required unique id between 1 and 2^32 - 1  
      # defaults to 1 if master-host is not set  
      # but will not function as a master if omitted  
      server-id   = 1  
    
    # Replication Slave (comment out master section to use this)  
    #  
    # To configure this host as a replication slave, you can choose between  
    # two methods :  
    #  
    # 1) Use the CHANGE MASTER TO command (fully described in our manual) -  
    #    the syntax is:  
    #  
    #    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,  
    #    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;  
    #  
    #    where you replace <host>, <user>, <password> by quoted strings and  
    #    <port> by the master's port number (3306 by default).  
    #  
    #    Example:  
    #  
    #    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,  
    #    MASTER_USER='joe', MASTER_PASSWORD='secret';  
    #  
    # OR  
    #  
    # 2) Set the variables below. However, in case you choose this method, then  
    #    start replication for the first time (even unsuccessfully, for example  
    #    if you mistyped the password in master-password and the slave fails to  
    #    connect), the slave will create a master.info file, and any later  
    #    change in this file to the variables' values below will be ignored and  
    #    overridden by the content of the master.info file, unless you shutdown  
    #    the slave server, delete master.info and restart the slaver server.  
    #    For that reason, you may want to leave the lines below untouched  
    #    (commented) and instead use CHANGE MASTER TO (see above)  
    #  
    # required unique id between 2 and 2^32 - 1  
    # (and different from the master)  
    # defaults to 2 if master-host is set  
    # but will not function as a slave if omitted  
    #server-id       = 2  
    #  
    # The replication master for this slave - required  
    #master-host     =   <hostname>  
    #  
    # The username the slave will use for authentication when connecting  
    # to the master - required  
    #master-user     =   <username>  
    #  
    # The password the slave will authenticate with when connecting to  
    # the master - required  
    #master-password =   <password>  
    #  
    # The port the master is listening on.  
    # optional - defaults to 3306  
    #master-port     =  <port>  
    #  
    # binary logging - not required for slaves, but recommended  
    #log-bin=mysql-bin  
    
      # Uncomment the following if you are using InnoDB tables  
      #innodb_data_home_dir = /usr/local/mysql/data  
      #innodb_data_file_path = ibdata1:10M:autoextend  
      #innodb_log_group_home_dir = /usr/local/mysql/data  
      # You can set .._buffer_pool_size up to 50 - 80 %  
      # of RAM but beware of setting memory usage too high  
      #innodb_buffer_pool_size = 16M  
      #innodb_additional_mem_pool_size = 2M  
      # Set .._log_file_size to 25 % of buffer pool size  
      #innodb_log_file_size = 5M  
      #innodb_log_buffer_size = 8M  
      #innodb_flush_log_at_trx_commit = 1  
      #innodb_lock_wait_timeout = 50  
    
        [mysqldump]  
        quick  
        max_allowed_packet = 16M  
    
          [mysql]  
          no-auto-rehash  
          # Remove the next comment character if you are not familiar with SQL  
          #safe-updates  
          default-character-set=utf8   
    
        [myisamchk]  
        key_buffer_size = 20M  
        sort_buffer_size = 20M  
        read_buffer = 2M  
        write_buffer = 2M  
    
          [mysqlhotcopy]  
          interactive-timeout
    

    3.保存文件

    mysql -u root

    image.png

    在MySQL 8.04前,执行:SET PASSWORD=PASSWORD('[新密码]');但是MySQL8.0.4开始,这样默认是不行的。因为之前,MySQL的密码认证插件是“mysql_native_password”,而现在使用的是“caching_sha2_password”。

    修改密码

    mysql> use mysql;

    mysql> update user set password=PASSWORD('新密码') where user="root";

    set password for 'root'@'localhost’=password('新密码');

    mysql>ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
    mysql> flush privileges;
    mysql> quit

    到这里root账户就已经重置成新的密码了。

    image.png

    Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException:

    image.png

    升级 10.12 Sierra 之后运行 brew update会报 /usr/local 无权写入
    10.12 之前 brew 使用 /usr/local 作为主目录,目录所有者是 user
    更新10.12之后 /usr/local 这个目录所有者变成了 root
    Fix:
    sudo chown -R $(whoami) /usr/local/sbin

    之后再运行 brew update 就能正常运行,同时 Homebrew 已经提供对 10.12的支持,更新后主目录迁移到 /usr/local/Homebrew ,现在再把权限改回来

    sudo chown -R $(whoami) /usr/local/sbin

    image.png

    可以看出redis服务器启动成功,并在监听6379端口的网络连接。
    注意: 使用命令$ redis-server也可以启动,此时并不会加载任何配置文件,使用的是程序中内置(built-in)的默认配置.

    检测redis服务器是否启动
    重新打开一个终端窗口,输入命令
    $ redis-cli ping
    该终端输出
    pong
    说明服务器运作正常。

    关闭redis
    关闭redis有2种方法:
    方法1
    在执行启动命令的终端窗口使用ctrl+c,此时第一个窗口输出

    8773:M 11 Sep 21:46:26.581 # User requested shutdown...
    8773:M 11 Sep 21:46:26.581 * Saving the final RDB snapshot before exiting.
    8773:M 11 Sep 21:46:26.583 * DB saved on disk
    8773:M 11 Sep 21:46:26.583 * Removing the pid file.
    8773:M 11 Sep 21:46:26.583 # Redis is now ready to exit, bye bye...
    然后在另外一个终端窗口执行$ redis-cli ping,输出
    Could not connect to Redis at 127.0.0.1:6379: Connection refused
    说明确实已关闭

    方法2
    在另外一个终端窗口执行$ redis-cli shutdown,此时第一个窗口输出

    8773:M 11 Sep 21:46:26.581 # User requested shutdown...
    8773:M 11 Sep 21:46:26.581 * Saving the final RDB snapshot before exiting.
    8773:M 11 Sep 21:46:26.583 * DB saved on disk
    8773:M 11 Sep 21:46:26.583 * Removing the pid file.
    8773:M 11 Sep 21:46:26.583 # Redis is now ready to exit, bye bye...
    然后在另外一个终端窗口执行$ redis-cli ping,输出
    Could not connect to Redis at 127.0.0.1:6379: Connection refused
    说明确实已关闭

    brew search nginx


    image.png

    有关brew常用的指令如下:

    1. brew搜索软件命令: brew search nginx
    2. brew安装软件命令: brew install nginx
    3. brew卸载软件命令: brew uninstall nginx
    4. brew升级命令: sudo brew update
    5. 查看安装信息(比如查看安装目录等) sudo brew info nginx
    6. 查看已经安装的软件:brew list

    brew info nginx

    image.png image.png

    nginx 是一款具有高负载能力的 web 服务器,也是 LNMP 架构的主要角色之一。现在越来越多的开发者选择 nginx 作为 php 的好搭档,替代 apache 的位置。下面我以 Mac 系统为例,介绍下 nginx 的配置

    基本配置
    打开 nginx.conf,找到 http 下的 server,前几行的内容分别是:

    listen 8080; #监听端口
    server_name localhost; #本地域名
    root /usr/local/var/www; #项目根目录
    nginx 默认监听8080端口,你可以改成 80 端口。默认项目根目录也可以更改。不过更改之后要重新载入配置文件才能生效:

    sudo nginx -s reload
    注意:如果你不喜欢localhost,你想要一个个性的本地域名,比如www.test.com,首先要编辑 hosts 文件:

    sudo vim /etc/hosts
    添加一条:

    127.0.0.1 www.test.com
    然后修改 nginx.conf:

    server_name www.test.com
    总之 nginx.conf 中设置的域名,必须在 hosts 文件中存在!

    常用命令
    查看版本,以及配置文件地址
    nginx -V
    查看版本
    nginx -v
    指定配置文件
    nginx -c filename
    帮助
    nginx -h
    重新加载配置|重启|停止|退出 nginx
    nginx -s reload|reopen|stop|quit
    -打开 nginx

    sudo nginx
    测试配置是否有语法错误
    nginx -t
    文件说明
    /usr/local/etc/nginx/nginx.conf (配置文件路径)

    /usr/local/var/www (服务器默认路径)

    /usr/local/Cellar/nginx/1.8.0 (安装路径
    修改nginx默认端口号
    cd /usr/local/etc/nginx/nginx.conf

    隐藏入口文件
    在我们开发项目的时候,一般会有隐藏入口文件的需求。依然是在 http 下的 server,我们找到location /,在大括号内做修改。切记ci框架和tp框架的写法稍有不同,具体如下:

    location / {
    index index.php index.html index.htm;
    if (!-e request_filename) { rewrite ^/(.*) /index.php?1 last; #ci框架写法 #rewrite ^/(.*) /index.php?s=/$1 last; #tp框架写法
    break;
    }
    }
    如果你用的是tp5, 入口文件在 public 目录下,但是你不想在URL中写localhost/public/访问入口文件,你想直接通过localhost/访问,你可以这样写:

    rewrite ^/(.*)/public/index.php?s=/1 last;
    其实隐藏入口文件就是 nginx 做了下路由,看懂它的正则,其实不难理解。

    多站点设置
    前面我们修改配置文件的代码位置,都是在 http 下的 server 里。其实一个 server 就相当于一个站点。nginx.conf 下 http 模块默认开启一个 server,就是只有一个站点。如果我们要多站点,就需要添加多个 server。

    现在我们要添加一个站点,假设这个站点的域名是 www.test2.com, 但是 server 不写在 nginx.conf 里。nginx 为我们提供了存放多站点配置文件的目录,我们切换到这个目录:

    cd /usr/local/etc/nginx/servers/
    然后新建配置文件:

    vim www.test2.com.conf
    里边写一个 server:

    server {

    listen  80;
    server_name  www.test2.com;
    
    index  index.html index.php;
    
    root  /usr/local/var/test2;
    
    location / {
         index  index.php index.html;
         rewrite ^/(.*)$ /public/index.php?s=/$1 last;
         break;
    }
    
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }
    

    }
    保存退出,重新载入配置文件。最后在 hosts 中添加:

    127.0.0.1 www.test2.com
    此时,www.test2.com 就可以访问到你的新项目目录 /usr/local/var/test2下了!

    反向代理设置
    我的应用场景是这样的。假设我的服务器上存放着一个 node 项目,node 占用着80端口,可以正常运行。但是我想再放一个 php 项目,此时这个php项目就不能是80端口了。如果想 node 项目和 php 项目均适用80端口,则需要利用 nginx 做反向代理设置。

    解决方案是,node 项目设置成非 80 端口,比如3000,nginx 添加一个站点,假设是www.test3.com,访问 80 端口,代理到 3000 端口即可。

    设置方法如上一节添加新站点:

    cd /usr/local/etc/nginx/servers/
    然后新建配置文件:

    vim www.test3.com.conf
    写入一个 server:

    server {
    listen 80;
    server_name www.test3.com;
    location / {
    proxy_pass http://127.0.0.1:3000;
    }
    }
    访问 www.test3.com,就可以访问到 node 项目了!

    相关文章

      网友评论

        本文标题:JDBC连接数据库时出现的Public Key Retrieva

        本文链接:https://www.haomeiwen.com/subject/iegszctx.html