情況介紹:在云上劃拉一塊地方建立本地數據庫測試環境,通過數據庫備份包恢復數據并啟動。
1.在云上或者你自己的server上安裝Percona Server for MySQL,步驟如下
Use APT repositories - Percona Server for MySQL
How to Install or Upgrade Percona Server for MySQL/MySQL 8 to a Specific Version on Debian/Ubuntu
$ sudo apt update
$ sudo apt install curl
$ curl -O https://repo.percona.com/apt/percona-release_latest.generic_all.deb
$ sudo apt install gnupg2 lsb-release ./percona-release_latest.generic_all.deb
$ sudo apt update
$ sudo percona-release setup ps80
$ sudo apt install percona-server-server
#這時輸入mysql就能進入了,如果想安裝指定的版本,可以用以下命令$ sudo percona-release setup ps80
$ sudo apt list -a percona-server-server
$ sudo apt install? percona-server-server=8.0.28-20-1.bullseye? percona-server-common=8.0.28-20-1.bullseye? percona-server-client=8.0.28-20-1.bullseye?
2.想辦法把生產環境的數據庫拷貝到這臺server上
3.檢查一下自己的my.cnf文件,修改datadir為你的備份的文件目錄,一般在/etc/my.cnf這個目錄,如果找不到可以執行這個下面的命令,然后到輸出來的路徑里找找看。
$ /usr/sbin/mysqld --help --verbose | grep -A1 'Default options are read from the following files in the given order'
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
你會找到一個類似這樣的文件
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
然后用你之前的用戶名密碼就可以登錄了,如果不能登錄又不知道密碼可以試試“--skip-grant-tables”方式,這樣開啟服務
$/etc/init.d/mysql start --skip-grant-tables
如果還是不行可以把“skip_grant_tables”加入你自己的my.cnf或者類似的文件里,就是包含datadir路徑的文件,然后重啟就可以免密登進mysql>了
4.修改密碼
如果是通過“skip_grant_tables”進去mysql>的可以用這個方式:
mysql登錄報錯:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) - 向前走。 - 博客園 (cnblogs.com)
因為這種情況下想修改密碼或者創建用戶的時候都會出現如下錯誤
mysql> ALTER USER IF EXISTS 'root'@'localhost' IDENTIFIED BY '123%' PASSWORD EXPIRE NEVER;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
上面文章中建議輸入flush privileges;我試過了,可以,進去后給自己建個賬號和新密碼。
mysql> CREATE USER 'your_username'@'%' IDENTIFIED BY 'your_password'; #創建用戶your_username
mysql> GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'%' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;mysql>ALTER USER 'your_username'@'%' IDENTIFIED WITH mysql_native_password BY 'your_password'; #這里更新一下密碼
mysql> FLUSH PRIVILEGES; #刷新權限
然后就可以用你喜歡的工具鏈接這個數據庫啦。Bye