Golang:http: panic serving [::1]:60007: runtime error: invalid memory address or nil pointer dereference

Error message as below:

🤡 http: panic serving [::1]:60007: runtime error: invalid memory address or nil pointer dereference

運行golang項目的時候,出現的問題,逐步排查吧,真的沒有辦法,我使用的是spew.Dump()函數打印的,最終找到了問題的梭子啊,排查的子問題如下:

Session err :session err : securecookie: the value is not valid 2023/08/10 17:55:51 securecookie: the value is not valid exit status 1

最近維護舊系統,在使用Golang的http服務處理用戶的請求的時候發現本地cookie有點問題,提示如上,

想起來在保存本地的調試環境是localhost:8080,在保存cookieName的時候,想著添加一個host作為後綴,結果出現了上述的後綴,於是處理了host,代碼如下:

host := request.Host //Caddy要传递相关参数.
    hostBoolean :=  strings.Contains(host,":")
    if hostBoolean {
        hostHeader := strings.Split(host, ":")[0]
        spew.Dump(host)
        port := strings.Split(host, ":")[1]
        spew.Dump(port)
        host =  hostHeader + port
        spew.Dump(host)
    }

但是在增加了上述的信息以後,還是在getCookie的時候,出現上面的問題,突然想到瀏覽器本地的cookie緩存(開發者模式)

golang panci

如圖,在找到該地方的內容以後,刪除即可。重新再請求,就沒有這個問題了

 

JavaScript Promise 回調地獄的解決方案

Promise對象的三種狀態

  1. pending 進行中
  2. fulfilled :已經成功
  3. rejected :已失敗

只有異步操作的結果,可以決定當前是哪一種狀態,任何其他的操作都無法改變這個狀態。

Promise 的特點

  1. 對象的狀態不受外界的影響。三種狀體,取決於異部操作的結果,然後決定了哪種狀態。
  2. 一旦状态改变,就不会再变,任何时候都可以得到这个结果。Promise 对象的状态改变,只有两种可能:从Pending 变成fulfilled和从Pendingrejected。只要这两种情况发生了,状态就不会再变了,一直保持这个结果,也就是成为resolved

參考文檔

Nodejs 之promise对象详解nodejs promise去宇宙给你摘星的博客-CSDN博客

IP-广播IP

什么是广播IP?广播IP是不是不好?

广播IP是一种技术手段,是云服务器运营商将其拥有IP地址段分配到服务器所在地的一种方式,因此广播IP的运营商所在国家和服务器的地理位置可以不一致。比如香港的运营商把其IP地址段广播到新加坡、菲律宾、印尼等地。广播IP的价格非常亲民,可以获得和原生IP一致的网络体验。广播IP和原生IP相比并没有好坏之分,还是要看用户具体需求和使用场景。

MYSQL error (appending)

1. not allow grant (mysql 8.x)

You are not allowed to create a user with GRANT

MYSQL8.0版本中,grant 命令已經做了一些限制,需要增加子用戶。

2. create user with ‘localhost’ limited.

create user 'username'@'localhost' identified by 'your password';
ERROR 1227 (42000): Access denied; you need (at least one of) the SYSTEM_USER privilege(s) for this operation

3. first install the mysql-server ‘s log

after you installed the my-server(8.0) ,the /var/log/mysql/error.log ‘s content as below :


2024-06-01T10:47:53.303275Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.36-0ubuntu0.22.04.1) initializing of server in progress as process 2524495
2024-06-01T10:47:53.308841Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-06-01T10:47:53.621206Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-06-01T10:47:55.308728Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2024-06-01T10:47:56.361302Z 6 [System] [MY-013172] [Server] Received SHUTDOWN from user boot. Shutting down mysqld (Version: 8.0.36-0ubuntu0.22.04.1).
2024-06-01T10:47:59.702238Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.36-0ubuntu0.22.04.1) starting as process 2524540
2024-06-01T10:47:59.723628Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-06-01T10:47:59.959131Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-06-01T10:48:00.209342Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2024-06-01T10:48:00.209368Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2024-06-01T10:48:00.213346Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/tmp' in the path is accessible to all OS users. Consider choosing a different directory.
2024-06-01T10:48:00.239500Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
2024-06-01T10:48:00.239617Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.36-0ubuntu0.22.04.1'  socket: '/tmp/tmp.x2knLvB42L/mysqld.sock'  port: 0  (Ubuntu).
2024-06-01T10:48:00.280022Z 0 [System] [MY-013172] [Server] Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.0.36-0ubuntu0.22.04.1).
2024-06-01T10:48:01.727897Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.36-0ubuntu0.22.04.1)  (Ubuntu).
2024-06-01T10:48:02.636676Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.36-0ubuntu0.22.04.1) starting as process 2524609
2024-06-01T10:48:02.643704Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-06-01T10:48:02.832343Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-06-01T10:48:03.028042Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2024-06-01T10:48:03.028072Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2024-06-01T10:48:03.032569Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/tmp' in the path is accessible to all OS users. Consider choosing a different directory.
2024-06-01T10:48:03.051732Z 7 [System] [MY-013172] [Server] Received SHUTDOWN from user boot. Shutting down mysqld (Version: 8.0.36-0ubuntu0.22.04.1).
2024-06-01T10:48:03.057668Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '127.0.0.1' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2024-06-01T10:48:04.552078Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.36-0ubuntu0.22.04.1)  (Ubuntu).

about the ‘—initialize-insecure’ introduction document link is Here.

https://dev.mysql.com/doc/refman/8.0/en/server-options.html#option_mysqld_initialize-insecure

if you want use the mysqld —initialize command to generate mysql database password, you may get the error in the /var/log/mysql/error.log :

[Server] --initialize specified but the data directory has files in it. Aborting.

so ,you must delete the data of the mysql data dir /var/lib/mysql

then exec the command :

mysqld —initialize

then you can get the mysql password in the log (/var/log/mysql/error.log);

2024-11-09T08:08:18.311176Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: /nuepoLfZ3=q

then start the mysql :

sudo service mysql restart 

or

/etc/init.d/mysql start 

use the random password login mysql .

when you exec the mysql command in mysql line mode, you will get the error :

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

try to use ‘alter user [root@localhost](<mailto:root@localhost>) identified by 'your new password' ’ to set the root ‘s password.

4. can’t create lock file /var/run/mysqld/mysqlx.sock.lock’

[ERROR] [MY-011300] [Server] Plugin mysqlx reported: 'Setup of socket: '/var/run/mysqld/mysqlx.sock' failed, can't create lock file /var/run/mysqld/mysqlx.sock.lock'

不能夠創建myqlx.sock.lock文件:

A message intended for a client cannot be sent there as no client-session is attached. Therefore, we’re sending the information to the error-log instead: MY-000029 – File ‘/usr/local/src/mysql_init‘ not found (OS errno 13 – Permission denied)

5. MSYQL reset root password

On Unix, use the following procedure to reset the password for the MySQL 'root'@'localhost' account. To change the password for a root account with a different host name part, modify the instructions to use that host name.

The instructions assume that you start the MySQL server from the Unix login account that you normally use for running it. For example, if you run the server using the mysql login account, you should log in as mysql before using the instructions. Alternatively, you can log in as root, but in this case you must start mysqld with the --user=mysql option. If you start the server as root without using --user=mysql, the server may create root-owned files in the data directory, such as log files, and these may cause permission-related problems for future server startups. If that happens, you must either change the ownership of the files to mysql or remove them.

  1. Log on to your system as the Unix user that the MySQL server runs as (for example, mysql).
  2. Stop the MySQL server if it is running. Locate the .pid file that contains the server’s process ID. The exact location and name of this file depend on your distribution, host name, and configuration. Common locations are /var/lib/mysql//var/run/mysqld/, and /usr/local/mysql/data/. Generally, the file name has an extension of .pid and begins with either mysqld or your system’s host name. Stop the MySQL server by sending a normal kill (not kill -9) to the mysqld process. Use the actual path name of the .pid file in the following command:

$> kill cat /mysql-data-directory/host_name.pid“ Use backticks (not forward quotation marks) with the cat command. These cause the output of cat to be substituted into the kill command. 3. Create a text file containing the password-assignment statement on a single line. Replace the password with the password that you want to use.

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass'; 4. Save the file. This example assumes that you name the file /home/me/mysql-init. The file contains the password, so do not save it where it can be read by other users. If you are not logged in as mysql (the user the server runs as), make sure that the file has permissions that permit mysql to read it. 5. Start the MySQL server with the init_file system variable set to name the file:

$> mysqld --init-file=/home/me/mysql-init & The server executes the contents of the file named by the init_file system variable at startup, changing the 'root'@'localhost' account password. Other options may be necessary as well, depending on how you normally start your server. For example, --defaults-file may be needed before the init_file argument. 6. After the server has started successfully, delete /home/me/mysql-init.

You should now be able to connect to the MySQL server as root using the new password. Stop the server and restart it normally.

MySQL :: MySQL 8.0 Reference Manual :: B.3.3.2 How to Reset the Root Password

6. exec mysqld —init-file=/home/ubuntu/mysql_init

the error log is :

2024-06-02T04:26:29.419188Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.36-0ubuntu0.22.04.1)  (Ubuntu).
2024-06-02T04:28:01.567754Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.36-0ubuntu0.22.04.1) starting as process 28222
2024-06-02T04:28:01.574355Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory.
2024-06-02T04:28:01.574414Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2024-06-02T04:28:01.574423Z 0 [ERROR] [MY-010119] [Server] Aborting
2024-06-02T04:28:01.574754Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.36-0ubuntu0.22.04.1)  (Ubuntu).

I delete all files of the dir /var/lib/mysql .

7. Mysql Server for Mac M1

start the mysql server .

You can go to the system settings . There is a MYSQL icon on the left side ,then click the Item. you will get the UI.

click the button 【Start MySQL Server 】 then Mysql will start .

When you use the brew start the MYSQL Server.

brew services start mysql 

then you will get the result .

Warning: Taking root:admin ownership of some mysql paths:
  /opt/homebrew/Cellar/mysql/8.3.0_1/bin
  /opt/homebrew/Cellar/mysql/8.3.0_1/bin/mysqld_safe
  /opt/homebrew/opt/mysql
  /opt/homebrew/opt/mysql/bin
  /opt/homebrew/var/homebrew/linked/mysql
This will require manual removal of these paths using `sudo rm` on
brew upgrade/reinstall/uninstall.
Warning: mysql must be run as non-root to start at user login!
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)

Then try connect the mysql Server .

ubuntu@hostname_mac Downloads % mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

The MYSQL Server doesn’t start .

 

PHP Development Note.

文章中紀錄在使用PHP項目開發中遇到的錯誤問題,但是這些問題在開發中經常碰到的日常問題,沒有必要老是去看文檔之類的解決問題,所以都總結到改文檔

Error Message 1:

Uncaught Swoole\Error: API must be called in the coroutine in

connect() to unix:/run/php/php8.1-fpm.sock failed (13: Permission denied) while connecting to upstream

解決方案:

查看該socket套接字的權限:

ls -l /run/php/php8.1-fpm.sock

發現權限是:

srw-rw---- 1 www-data www-data 0 Sep  6 15:04 /run/php/php8.1-fpm.sock

查看php-fpm的配置文件,由於使用的php-fpmpool管理,所以要查看pool.d中的配置如下:

; Set permissions for unix socket, if one is used. In Linux, read/write

; permissions must be set in order to allow connections from a web server. Many

; BSD-derived systems allow connections regardless of permissions. The owner

; and group can be specified either by name or by their numeric IDs.

; Default Values: user and group are set as the running user

;                 mode is set to 0660

listen.owner = ubuntu

;listen.owner = www-data

listen.group = ubuntu

;listen.owner = www-data

Error Message 2:

今日在安裝部署PHP8.1-FPM的時候,nginx配置沒有問題,但是頻繁出現FILE NOT FOUND的提示。

nginx的日誌提示:

[error] 4805#4805: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 141.101.86.98, server: ethanwong.online, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.1-fpm.sock:", host: "ethanwong.online"

解決方案是修改PHP-FPM的配置:

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = www-data
group = www-data

修改為:

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = ubuntu
group = ubuntu

其中ubuntu 是當前登錄的Ubuntu系統的子用戶。

修改之後重啟php8.1-fpm問題就解決了。