1.檢測系統內部有沒有安裝其他的mysql數據庫
rpm -qa | grep mysql
然后如果有的話刪除這些mysql yum remove 查出來的所有名字
2.徹底刪除系統中mysql的目錄
find / -name mysql
將查出的所有目錄刪掉 rm -rf 查到的路徑
3.下載mysql的rpm包
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
- 1
4.安裝mysql源
yum localinstall mysql57-community-release-el7-8.noarch.rpm
- 1
5.重要一步
先嘗試安裝
yum install -y mysql-community-server
- 1
如果可以安裝那么安裝之后查詢mysql版本 mysql -V
顯示5.7版本則安裝成功
如果安裝成立5.6
那么修改mqsql源里面的一些 vi /etc/yum.repos.d/mysql-community.repo :比如要安裝5.6版本,將5.7源的enabled=1改成enabled=0。然后再將5.6源的enabled=0改成enabled=1即可。
這里是將5.7的修改為1 5.6的修改為0
# Enable to use MySQL 5.5 [mysql55-community] name=MySQL 5.5 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # Enable to use MySQL 5.6 [mysql56-community] name=MySQL 5.6 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/ enabled=0 ##################################### gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ enabled=1 ####################################### gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-tools-preview] name=MySQL Tools Preview baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/ enabled=0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
再次安裝 yum install mysql-community-server
6.啟動mysql服務
systemctl start mysqld
- 1
7.mysql開啟啟動
systemctl enable mysqld
- 1
8.查看mysql密碼
grep 'temporary password' /var/log/mysqld.log
- 1
9.如果你什么都沒有查到
那么默認密碼是沒有的
直接進入mysql
mysql -uroot -p
- 1
10.修改密碼
***注意這里數據庫為mysql5.7或以上password字段修改成了authentication_string;
set password = passowowd('你的密碼');
- 1
如果你在這一步發生錯誤,那么請退出mysql后使用命令先更新一下數據庫
mysql_upgrade -uroot -p
- 1
然后再次進入mysql設置root密碼即可
如果還是不行的話,就使用下面方法
查看root密碼
grep 'temporary password' /var/log/mysqld.log
- 1
查看后打開mysql並修改密碼
mysql -uroot -p
首次修改密碼會提示密碼安全等級的錯誤,因此要先修改安全等級,再次修改才會生效
1、設置安全級別 set global validate_password_policy=0; 2、默認密碼為8,可以設置為其他值,最小4位 set global validate_password_length=4; 3、設置root密碼 set password for root@localhost = password('新密碼');
- 1
- 2
- 3
- 4
- 5
- 6
11.開啟mysql的root賬戶遠程訪問
- 進入mysql
mysql -uroot -p 輸入密碼登錄
- 1
- 選擇mysql數據庫
use mysql;
- 1
- 修改root遠程訪問
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
- 1
4.更新數據庫
FLUSH PRIVILEGES;
- 1
注意這個時候防火牆要關閉,或者開放3306端口
二、導入數據庫
1、首先建空數據庫
mysql>create database abc;
2、導入數據庫
方法一:
(1)選擇數據庫
mysql>use abc;
(2)設置數據庫編碼
mysql>set names utf8;
(3)導入數據(注意sql文件的路徑)
mysql>source /home/abc/abc.sql;
方法二:
mysql -u用戶名 -p密碼 數據庫名 < 數據庫名.sql
mysql -uabc_f -p abc < abc.sql
如果安裝成功報錯
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this st
修改my.cnf配置 sed -i ‘/mysqld/a\skip-grant-tables’ /etc/my.cnf
-
重啟mysql
systemctl restart mysqld
-
進入mysql
這次就不需要root密碼了
-
use mysql;
-
UPDATE user SET Password = password('newpassword') where User = 'roo
-
刷新權限
flush privileges;
-
退出
quit
-
還原my.cnf配置sed -i 's/skip-grant-tables/#skip-grant-tables/g' /etc/my.cnf
-
再次重啟mysql服務
systemctl restart mysqld
1
|
mysql> update user
set
password=password(“新密碼”) where user=”用戶名”;
|
執行后報錯 ERROR 1054(42S22) Unknown column 'password' in ‘field list’
錯誤的原因是 5.7版本下的mysql數據庫下已經沒有password這個字段了,password字段改成了authentication_string
所以請使用一下命令:
>mysql -u root -p Enter password: ******** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 5.7.18-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. 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> use mysql; Database changed mysql> select User from user; #此處為查詢用戶命令 +-----------+ | User | +-----------+ | ******* | | mysql.sys | | root | +-----------+ 3 rows in set (0.00 sec) mysql> update user set password=password("*******") where user="*******"; #修改密碼報錯 ERROR 1054 (42S22): Unknown column 'password' in 'field list' mysql> update mysql.user set authentication_string=password('*******') where user='*******'; #修改密碼成功 Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> flush privileges; #立即生效 Query OK, 0 rows affected (0.00 sec) mysql> quit Bye n>mysql -u ******* -p #以該用戶登錄成功. Enter password: ******** ………………………… mysql>