MySQL在Centos7環(huán)境安裝MySQL教程
說明:
安裝與卸載中,??全部切換成為root,?旦 安裝,普通??能使?的
一、卸載內(nèi)置環(huán)境
1、卸載不要的環(huán)境
[whb@VM-0-3-centos ~]$ ps ajx |grep mariadb # 先檢查是否有mariadb存在 13134 14844 14843 13134 pts/0 14843 S+ 1005 0:00 grep --color=auto mariadb 19010 19187 19010 19010 ? -1 Sl 27 16:55 /usr/libexec/mysqld -- basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin -- log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock [whb@VM-0-3-centos ~]$ systemctl stop mariadb.service # 停?mariadb 服務(wù) ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === Authentication is required to manage system services or units. Authenticating as: root Password: ==== AUTHENTICATION COMPLETE === [whb@VM-0-3-centos ~]$ ps axj |grep mariadb # 停?完成 13134 14976 14975 13134 pts/0 14975 S+ 1005 0:00 grep --color=auto mariadb
mariadb 其實(shí)就是 mysql,只不過是 mysql 的另一種拉出來的開源分支,也可以正常使用,如果不想卸載也是可以的。
2、檢查系統(tǒng)安裝包
[whb@VM-0-3-centos ~]$ rpm -qa | grep mariadb or [whb@VM-0-3-centos ~]$ rpm -qa | grep mysql ... ... ... # 下?是打印出來的mysql的樣例, [root@bite-alicloud mysql]# rpm -qa | grep mysql mysql-community-common-5.7.41-1.el7.x86_64 mysql-community-server-5.7.41-1.el7.x86_64 mysql57-community-release-el7-11.noarch mysql-community-client-5.7.41-1.el7.x86_64 mysql-community-libs-5.7.41-1.el7.x86_64
3、卸載這些默認(rèn)的安裝包
#卸載顯?出來的mariadb/mysql安裝包 [whb@VM-0-3-centos ~]$ sudo yum remove mariadb # 還是試著將上?的包都?個(gè)?個(gè)卸載吧 備份/etc/my.cnf,備份/var/lib/mysql數(shù)據(jù)
4、獲取 mysql 官方 yum 源
下載 MySQL yum包:
wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
#1. 獲取mysql官方y(tǒng)um源 http://repo.mysql.com/ # 一定要看好下面的文字!如果后面自己安裝不成功,就看看這個(gè) # 注意:最好安裝和自己系統(tǒng)一致的mysql版本,否則可能會(huì)存在軟件兼容性問題 # 查看自己的系統(tǒng)版本 # [root@VM-8-5-centos ~]# cat /etc/redhat-release # CentOS Linux release 7.8.2003(Core) # 可以進(jìn)入 http://repo.mysql.com/,找一下和自己版本一致的資源 # 下載到你的本地,然后上傳到你的Linux服務(wù)器
二、 配置 MySQL yum 源
1、安裝 mysql yum 源,對(duì)比前后 yum 源
安裝 MySQL 源:rpm -Uvh mysql57-community-release-el7-10.noarch.rpm
安裝前 [root@VM-8-5-centos MySQL]$ ls mysql57-community-release-el7-10.noarch.rpm 查看你本地的yum源 [每個(gè)人的都不同,沒有mysql相關(guān)的yum源] [root@VM-8-5-centos MySQL]$ ls /etc/yum.repos.d/ -al total 96 drwxr-xr-x. 2 root root 4096 Apr 12 11:04 . drwxr-xr-x. 106 root root 12288 Apr 12 10:50 .. -rw-r--r-- 1 root root 2523 Dec 26 19:31 Centos-Base.repo -rw-r--r-- 1 root root 614 Nov 10 18:42 CentOS-Base.repo_bak -rw-r--r-- 1 root root 2523 Jun 16 2018 CentOS-Base.repo.bak -rw-r--r-- 1 root root 1309 Apr 8 2020 CentOS-CR.repo -rw-r--r-- 1 root root 649 Apr 8 2020 CentOS-Debuginfo.repo -rw-r--r-- 1 root root 230 Nov 10 18:42 CentOS-Epel.repo -rw-r--r-- 1 root root 314 Apr 8 2020 CentOS-fasttrack.repo -rw-r--r-- 1 root root 630 Apr 8 2020 CentOS-Media.repo -rw-r--r-- 1 root root 998 Dec 11 2018 CentOS-SCLo-scl.repo -rw-r--r-- 1 root root 971 Oct 29 2018 CentOS-SCLo-scl-rh.repo -rw-r--r-- 1 root root 1331 Apr 8 2020 CentOS-Sources.repo -rw-r--r-- 1 root root 7577 Apr 8 2020 CentOS-Vault.repo -rw-r--r-- 1 root root 616 Apr 8 2020 CentOS-x86_64-kernel.repo -rw-r--r-- 1 root root 1919 Apr 8 23:57 docker-ce.repo -rw-r--r-- 1 root root 1050 Nov 1 04:33 epel.repo -rw-r--r-- 1 root root 1149 Nov 1 04:33 epel-testing.repo -rw-r--r-- 1 root root 173 Dec 9 16:08 google-chrome.repo -rw-r--r-- 1 root root 1627 Apr 5 2017 mysql-community.repo #安裝mysql yum源 -rw-r--r-- 1 root root 1663 Apr 5 2017 mysql-community-source.repo
2、再看看能不能正常工作
[root@VM-8-5-centos MySQL]$ yum list |grep mysql Repository epel is listed more than once in the configuration mysql57-community-release.noarch el7-10 installed akonadi-mysql.x86_64 1.9.2-4.el7 base anope-mysql.x86_64 2.0.9-3.el7 epel apr-util-mysql.x86_64 1.5.2-6.el7 base calligra-kexi-driver-mysql.x86_64 2.9.10-2.el7 epel collectd-mysql.x86_64 5.8.1-1.el7 epel dmlite-plugins-mysql.x86_64 1.14.2-1.el7 epel dovecot-mysql.x86_64 1:2.2.36-8.el7 base
一開始的時(shí)候我們安裝的 yum,在安裝完之后是可以選擇刪除的,因?yàn)槊看?yum 操作都會(huì)更新一次,耗費(fèi)時(shí)間。
rpm -qa | grep mysql
yum -y remove mysql57-community-release-el7-10.noarch
三、開始安裝
1、安裝 mysql 服務(wù)
安裝 MySQL 服務(wù)端,需要等待一些時(shí)間:yum install -y mysql-community-server
[root@VM-8-5-centos MySQL]$ sudo yum install -y mysql-community-server [sudo] password for root: Loaded plugins: aliases, auto-update-debuginfo, fastestmirror, protectbase Repository epel is listed more than once in the configuration ... Installing : mysql-community-common-5.7.33-1.el7.x86_64 #公共模塊 Installing : mysql-community-libs-5.7.33-1.el7.x86_64 #庫 Installing : mysql-community-client-5.7.33-1.el7.x86_64 #客戶端 Installing : mysql-community-server-5.7.33-1.el7.x86_64 #服務(wù)器 ... #安裝了服務(wù)和客戶端 # 如果報(bào)錯(cuò)了,看第16步《常見問題》
2、查看配置文件和數(shù)據(jù)存儲(chǔ)位置
[root@VM-8-5-centos MySQL]$ ls /etc/my.cnf /etc/my.cnf [root@VM-8-5-centos MySQL]$ sudo ls /var/lib/mysql ....
3、啟動(dòng)服務(wù)
啟動(dòng) MySQL:systemctl start mysqld.service
[root@VM-8-5-centos MySQL]$ systemctl start mysqld.service ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === Authentication is required to manage system services or units. Authenticating as: root Password: ==== AUTHENTICATION COMPLETE ===
4、查看啟動(dòng)服務(wù)
檢查是否啟動(dòng)成功:systemctl status mysqld.service
[root@VM-8-5-centos MySQL]$ ps axj |grep mysqld 1 23183 23182 23182 ? -1 Sl 27 0:00 /usr/sbin/mysqld --daemonize -- pid-file=/var/run/mysqld/mysqld.pid 13134 23243 23242 13134 pts/0 23242 S+ 1005 0:00 grep --color=auto mysqld [root@VM-8-5-centos MySQL]$ sudo ls -al /var/lib/mysql total 122956 drwxr-x--x 5 mysql mysql 4096 Apr 12 11:23 . drwxr-xr-x. 39 root root 4096 Apr 12 11:11 .. -rw-r----- 1 mysql mysql 56 Apr 12 11:23 auto.cnf -rw------- 1 mysql mysql 1680 Apr 12 11:23 ca-key.pem -rw-r--r-- 1 mysql mysql 1112 Apr 12 11:23 ca.pem -rw-r--r-- 1 mysql mysql 1112 Apr 12 11:23 client-cert.pem -rw------- 1 mysql mysql 1676 Apr 12 11:23 client-key.pem -rw-r----- 1 mysql mysql 436 Apr 12 11:23 ib_buffer_pool -rw-r----- 1 mysql mysql 12582912 Apr 12 11:23 ibdata1 -rw-r----- 1 mysql mysql 50331648 Apr 12 11:23 ib_logfile0 -rw-r----- 1 mysql mysql 50331648 Apr 12 11:23 ib_logfile1 -rw-r----- 1 mysql mysql 12582912 Apr 12 11:23 ibtmp1 drwxr-x--- 2 mysql mysql 4096 Apr 12 11:23 mysql srwxrwxrwx 1 mysql mysql 0 Apr 12 11:23 mysql.sock -rw------- 1 mysql mysql 6 Apr 12 11:23 mysql.sock.lock drwxr-x--- 2 mysql mysql 4096 Apr 12 11:23 performance_schema -rw------- 1 mysql mysql 1680 Apr 12 11:23 private_key.pem -rw-r--r-- 1 mysql mysql 452 Apr 12 11:23 public_key.pem -rw-r--r-- 1 mysql mysql 1112 Apr 12 11:23 server-cert.pem -rw------- 1 mysql mysql 1680 Apr 12 11:23 server-key.pem drwxr-x--- 2 mysql mysql 12288 Apr 12 11:23 sys
四、開始登陸
1、登陸方法一【不行就下一個(gè)】
獲取臨時(shí)密碼,MySQL5.7 為 root 用戶隨機(jī)生成了一個(gè)密碼:
grep 'temporary password' /var/log/mysqld.log
#獲取臨時(shí)root密碼 [root@VM-8-5-centos MySQL]$ sudo grep 'temporary password' /var/log/mysqld.log 2021-04-12T03:23:46.153263Z 1 [Note] A temporary password is generated for root@localhost: yLMalT:v+5l* #使用臨時(shí)密碼登錄 [root@VM-8-5-centos MySQL]$ mysql -uroot -p Enter password: #判斷修改密碼時(shí)候新密碼是否符合當(dāng)前的策略,不滿足報(bào)錯(cuò),不讓修改,關(guān)閉它 #安全強(qiáng)度,默認(rèn)為中,即1,要求必須包含 數(shù)字、符號(hào)、大小寫字母,長度至少為8位 mysql> set global validate_password_policy=0; Query OK, 0 rows affected (0.00 sec) #密碼最小長度 mysql> set global validate_password_length=1; Query OK, 0 rows affected (0.00 sec) #修改本地登錄密碼,暫不授權(quán)遠(yuǎn)程登錄 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) # 如果你安裝最新的mysql,沒有所謂的臨時(shí)密碼,root默認(rèn)沒有密碼
通過臨時(shí)密碼登錄 MySQL,進(jìn)行修改密碼操作:mysql -uroot -p
使用臨時(shí)密碼登錄后,不能進(jìn)行其他的操作,否則會(huì)報(bào)錯(cuò),這時(shí)候我們進(jìn)行修改密碼操作。
因?yàn)?MySQL 的密碼規(guī)則需要很復(fù)雜,我們一般自己設(shè)置的不會(huì)設(shè)置成這樣,所以我們?nèi)中薷囊幌拢?/p>
mysql> set global validate_password_policy=0; mysql> set global validate_password_length=1;
這時(shí)候我們就可以自己設(shè)置想要的密碼了。
ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword';
2、登陸方法二【不行就下一個(gè)】
# 如果你安裝的最新的mysql,沒有所謂的臨時(shí)密碼,root默認(rèn)沒有密碼 # 試著直接client登陸一下
3、登陸方法三【應(yīng)該行了吧】
[root@VM-8-5-centos MySQL]# vim /etc/my.cnf # 打開mysql配置文件 在[mysqld]最后一欄配置(不知道是什么,就放在配置文件最后) 加入:skip-grant-tables 選項(xiàng),并保存退出 [root@VM-8-5-centos MySQL]# systemctl restart mysqld # 重啟mysql服務(wù) # 登陸成功 [root@VM-8-5-centos MySQL]# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.7.44 MySQL Community Server (GPL) Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
五、設(shè)置配置文件
1、設(shè)置開機(jī)啟動(dòng)【可以不設(shè)】
先退出 mysql 命令行,然后輸入以下命令:
#開啟開機(jī)自啟動(dòng) systemctl enable mysqld systemctl daemon-reload
2、配置 my.cnf
設(shè)置 MySQL 的字符集為 UTF-8,令其支持中文:
vim /etc/my.cnf
#配置一下my.conf,主要是數(shù)據(jù)庫客戶端和服務(wù)器的編碼格式 [root@VM-8-5-centos MySQL]$ cat /etc/my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M port=3306 character-set-server=utf8 default-storage-engine=innodb datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 character-set-server=utf8 default-storage-engine=innodb log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid skip-grant-tables # 配置完畢,重啟mysql即可
重啟一下 MySQL,令配置生效:
- 重啟 mysql:service mysqld restart
- or
- 停止 mysql:service mysqld stop
- 啟動(dòng) mysql:service mysqld start
3、常見問題
mysql 已經(jīng)配置了客戶端服務(wù)器utf8編碼,但是無法輸入中文 確保您在終端命令行中可以輸入中文 [root@VM-8-5-centos ~]$ env | grep LANG LANG=en_US.utf8
安裝遇到秘鑰過期的問題: Failing package is: mysql-community-client-5.7.39-1.EL7.86_64 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 解決方案: rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于MySQL的sql_mode合理設(shè)置詳解
這篇文章主要給大家介紹了關(guān)于MySQL的sql_mode合理設(shè)置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07解讀sql中timestamp和datetime之間的轉(zhuǎn)換
這篇文章主要介紹了解讀sql中timestamp和datetime之間的轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12mysql 日期和時(shí)間格式轉(zhuǎn)換實(shí)現(xiàn)語句
對(duì)于每個(gè)類型擁有的值范圍以及并且指定日期何時(shí)間值的有效格式的描述見7.3.6 日期和時(shí)間類型。2009-10-10Windows 10系統(tǒng)下徹底刪除卸載MySQL的方法教程
mysql數(shù)據(jù)庫的重新安裝是一個(gè)麻煩的問題,很難卸除干凈,下面這篇文章主要給大家介紹了關(guān)于在Windows 10系統(tǒng)下徹底刪除卸載MySQL的方法教程,對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07MySQL中實(shí)現(xiàn)行列轉(zhuǎn)換的操作示例
在 MySQL 中進(jìn)行行列轉(zhuǎn)換(即,將某些列轉(zhuǎn)換為行或?qū)⒛承┬修D(zhuǎn)換為列)通常涉及使用條件邏輯和聚合函數(shù),本文給大家介紹了MySQL中實(shí)現(xiàn)行列轉(zhuǎn)換的操作示例,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-06-06MySQL數(shù)據(jù)庫的多種連接方式以及實(shí)用工具
mysql連接操作是客戶端進(jìn)程與mysql數(shù)據(jù)庫實(shí)例進(jìn)程進(jìn)行通信,下面這篇文章主要給大家介紹了關(guān)于MySQL數(shù)據(jù)庫的多種連接方式以及實(shí)用工具的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02