操作系統版本:
openEuler release 22.03 (LTS-SP4)
MySql版本:
下載地址:
https://dev.mysql.com/downloads/mysql/
準備安裝:
關閉防火墻:
停止防火墻
#systemctl stop firewalld.service
關閉防火墻
#systemctl disable firewalld.service
執行disable命令關閉防火墻的同時,也取消了開機自啟動。
查看防火墻狀態
#systemctl status firewalld.service
mysql安裝好后 ,如果需要開啟防火墻,請執行下面2個命令:
#firewall-cmd --permanent --add-port=3306/tcp
#firewall-cmd --reload
關閉SELinux:
SElinux對服務的影響:SElinux是最全面的安全系統,若不關閉會對文件和服務有一定的限制
設置SELinux成為permissive模式,臨時關閉SElinux。
#setenforce 0
查看SELinux狀態,確認為permissive模式。
#getenforce
也可以永久關閉SELinux
#vi /etc/selinux/config
將SELINUX=enforcing改為SELINUX=disabled
上傳安裝包:
把下載下來的安裝包上傳到服務器:/opt/software/mysql目錄中
解壓安裝包:
#tar -xvf mysql-8.4.5-linux-glibc2.28-x86_64.tar.xz
創建用戶與用戶組:
#groupadd -g 1201 mysql
#useradd -g mysql -u 1201 -m mysql
設置mysql用戶密碼:
#passwd mysql #回車輸入兩次密碼
修改目錄owner:
#chowner -R mysql:mysql /opt/software/mysql
配置環境變量:
#vi /etc/profile
追加:
export PATH=$PATH:/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/bin
初始化數據庫:
進入bin目錄
執行初始化命令:
#./mysqld --initialize --user=mysql --port=3306 --basedir=/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64 --datadir=/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/data --plugin-dir=/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/lib/plugin --log-error=/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/logs/mysqld_error.log --open-files-limit=10240 --pid-file=/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/mysqld.pid --socket=/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/mysql.sock
?請記住生成的root用戶臨時密碼,后面登錄mysql會用到
也可以將初始化參數寫入配置文件my.cnf中,然后通過指定初始化配置文件的方式初始化安裝mysql,第一個參數必須是?defaults-file
,指向準備好的my.cnf
?配置文件,第二個參數是?initialize
,指示?mysqld
?進行初始化操作。
例如:
#./mysqld --defaults-file=/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/my.cnf --initialize
復制MySQL服務腳本(可選):?
如果你希望使用系統服務管理MySQL(例如使用systemctl),可以復制MySQL提供的系統服務腳本到/etc/systemd/system/目錄:?
#cp support-files/mysql.server /etc/init.d/mysql
?開啟mysql管理服務命令:
#systemctl enable mysql?
啟動mysql服務:
#./mysqld_safe --user=mysql &
或者:
#systemctl start mysql
安全設置:
#mysql_secure_installation
會提示修改root用戶密碼,是否要設置驗證密碼組件,是否移除匿名用戶,是否禁止root賬戶遠程登錄,是否刪除test數據庫及其訪問權限,是否立即重新加載權限表(選y)等
[root@mysqlHost bin]# mysql_secure_installationSecuring the MySQL server deployment.Enter password for user root:The existing password for the user account root has expired. Please set a new password.New password:Re-enter new password:VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?Press y|Y for Yes, any other key for No: no
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : yNew password:Re-enter new password:
Sorry, passwords do not match.New password:Re-enter new password:
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.Remove anonymous users? (Press y|Y for Yes, any other key for No) : no... skipping.Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.Disallow root login remotely? (Press y|Y for Yes, any other key for No) : no... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.Remove test database and access to it? (Press y|Y for Yes, any other key for No) : no... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.All done!
登錄mysql:
#mysql -u root -p -P 3306
常遇到的問題:
1. 登錄mysql時報錯:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
解釋/原因:這個提示是缺失mysql.sock文件,安裝mysql后沒有做安全設置,即沒有執行mysql_secure_installation命令進行一系列的設置,就登錄mysql,這樣就會報這個錯誤,這個文件是在執行mysql_secure_installation命令后,才會產生。
如果沒有進行安全設置就想登錄,可以用這個方法:
./mysqld_safe --user=mysql --basedir=/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64 --datadir=/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/data &
然后再登錄即可
my.cnf配置說明:
MySql8.4沒有默認的my.cnf,需要用戶根據需要自行配置my.cnf文件
[client]
# 客戶端連接 MySQL 服務器的端口號,通常是 3306。
port = 3306
# MySQL 服務器的套接字文件路徑,用于本地連接。
socket =/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/mysql.sock
#編碼
default-character-set = utf8mb4[mysqld]
#Mysql服務的唯一編號 每個mysql服務Id需唯一
server-id=1
# MySQL 服務器監聽的端口號,通常也是 3306。
port = 3306
# MySQL 服務器的套接字文件路徑,用于本地連接。
socket = /opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/mysql.sock
# MySQL 的根目錄路徑,通常用于安裝 MySQL 的根目錄(default /usr)
basedir = /opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64
# mysql存放數據庫文件的目錄路徑,通常與basedir不在一個文件夾內
datadir=/var/lib/mysql
# 啟用binglog日志文件,可以指定目錄,如果不指定則放在數據目錄下面
log_bin = mysql-bin
#存放 MySQL 進程 ID 的文件路徑。
pid-file = /opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/data/mysql.pid
#錯誤日志路徑
log_error = /opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/logs/mysql-error.log
#臨時數據路徑
tmpdir=/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/tmp
#用于指定 MySQL 服務器綁定的 IP 地址,0.0.0.0 表示綁定到所有可用的 IP 地址。
bind-address = 0.0.0.0
# 跳過密碼登錄
#skip-grant-tables
#事務隔離級別,默認為可重復讀,mysql默認可重復讀級別(此級別下可能參數很多間隙鎖,影響性能)
#transaction_isolation=READ-COMMITTED
# 連接到 MySQL 服務器時初始化 SQL 命令。
init-connect = 'SET NAMES utf8mb4'
# 服務器默認的字符集。
character-set-server = utf8mb4
#允許在內核中等待的連接數量
back_log = 300
# 允許的最大并發連接數。
max_connections = 1000
# 最大連接錯誤數
max_connect_errors = 6000
# 打開的文件數限制。
open_files_limit = 65535
# 表緩存大小。
table_open_cache = 128
# 單個查詢的最大允許數據包大小
max_allowed_packet = 4M
# 二進制日志緩存大小
binlog_cache_size = 1M#內部內存臨時表的最大值 ,設置成128M。
#比如大數據量的group by ,order by時可能用到臨時表,
#超過了這個值將寫入磁盤,系統IO壓力增大
# 最大堆表大小
max_heap_table_size = 8M
# 臨時表大小
tmp_table_size = 16M# 讀取緩沖區大小
read_buffer_size = 2M
# 隨機讀取緩沖區大小
read_rnd_buffer_size = 8M
# 排序緩沖區大小
sort_buffer_size = 8M
# 連接緩沖區大小
join_buffer_size = 8M
# 鍵緩沖區大小
key_buffer_size = 4M
# 線程緩存大小
thread_cache_size = 8#禁用mysql的緩存查詢結果集功能
#后期根據業務情況測試決定是否開啟
#大部分情況下關閉下面兩項
# 查詢緩存類型 (1 表示啟用)
#query_cache_type = 0
# 查詢緩存大小
#query_cache_size = 8M
# 查詢緩存限制
#query_cache_limit = 2M# 全文索引最小詞長度
ft_min_word_len = 4
# 二進制日志文件的格式
binlog_format = mixed# 二進制日志文件自動清理天數
expire_logs_days = 30#慢查詢sql日志設置
# 啟用慢查詢日志 (1 表示啟用)
#slow_query_log = 1
#慢查詢sql日志路徑
#slow_query_log_file = /opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/logs/mysql-slow.log
# 定義慢查詢的閾值時間
#long_query_time = 1#檢查未使用到索引的sql
log_queries_not_using_indexes=1# 性能模式 (0 表示禁用)
performance_schema = 0
#明確指定 MySQL 是否應該使用嚴格的模式來檢查日期和時間值:TIMESTAMP如果沒有顯示聲明NOT NULL,允許NULL值
explicit_defaults_for_timestamp=true
# 表名大小寫不敏感 (1 表示啟用:不區分大小寫)
lower_case_table_names = 1
# 禁用外部鎖定,用于控制表級鎖定
skip-external-locking
# 默認存儲引擎 (InnoDB)
default_storage_engine = InnoDB
# 每個表使用單獨的 InnoDB 文件
innodb_file_per_table = 1
# InnoDB 可以打開的最大文件數
innodb_open_files = 500
# InnoDB 緩沖池大小
innodb_buffer_pool_size = 64M
# InnoDB 寫 I/O 線程數
innodb_write_io_threads = 4
# InnoDB 讀 I/O 線程數
innodb_read_io_threads = 4
# InnoDB 線程并發度
innodb_thread_concurrency = 0
# InnoDB 清理線程數
innodb_purge_threads = 1
# InnoDB 日志刷新行為
innodb_flush_log_at_trx_commit = 2
# InnoDB 日志緩沖大小
innodb_log_buffer_size = 2M
# InnoDB 日志文件大小
innodb_log_file_size = 32M
# InnoDB 日志文件組數
innodb_log_files_in_group = 3
# InnoDB 最大臟頁百分比
innodb_max_dirty_pages_pct = 90
# InnoDB 鎖等待超時時間
innodb_lock_wait_timeout = 120
# 批量插入緩沖區大小
bulk_insert_buffer_size = 8M
#MySQL連接閑置超過一定時間后(單位:秒)將會被強行關閉
#MySQL默認的wait_timeout 值為8個小時, interactive_timeout參數需要同時配置才能生效
# 交互超時時間
interactive_timeout = 28800
# 等待超時時間
wait_timeout = 28800
[mysqldump]
quick
# mysqldump 最大允許數據包大小,如果有BLOB對象建議修改成1G
max_allowed_packet = 100M
[myisamchk]
# MyISAM 檢查工具的鍵緩沖區大小
key_buffer_size = 8M
# MyISAM 檢查工具的排序緩沖區大小
sort_buffer_size = 8M
# 讀緩存大小
read_buffer = 4M
# 寫緩存大小
write_buffer = 4M
mysql bin目錄幾個常見的服務:
mysql
: MySQL客戶端程序,用于連接和管理MySQL服務器。mysqldump
: 用于備份MySQL數據庫的工具。mysqladmin
: 用于執行管理操作的MySQL客戶端工具。mysqld
: MySQL服務器的主程序。mysql_config
: 用于獲取MySQL編譯配置信息的工具。mysql_secure_installation
: 用于增強新安裝MySQL服務器安全性的腳本。
忘記密碼的解決辦法:
忘記密碼的處理辦法,通過指定的參數啟動?mysql
,執行?flush privileges
?命令,加載用戶和權限相關的表,修改密碼后重新啟動
--skip-networking
?選項禁用了MySQL
的網絡功能,這意味著只能通過本地連接
(例如使用localhost或127.0.0.1)訪問MySQL服務器。
?#./mysqld_safe --defaults-file=/opt/software/mysql/mysql-8.4.5-linux-glibc2.28-x86_64/my.cnf --skip-grant-tables --skip-networking &
--skip-grant-tables
?選項使得MySQL
服務器啟動時跳過權限
表的加載,這意味著任何人都可以連接到?MySQL服務器
而無需密碼。
然后用root賬號登錄mysql數據庫
#mysql -uroot
然后修改root賬號密碼
#alter user 'root'@'localhost' identified by 'xxx'??
退出mysql,重新用root和設置的新密碼登錄