Centos 7 可安裝 mysql5.7

1、安裝配置MySQL的yum源html

安裝MySQL的yum源,下面是RHEL6系列mysql5.6的下載地址:

wget http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm 下面是RHEL6系列mysql5.7的下載地址: wget http://repo.mysql.com//mysql57-community-release-el6-8.noarch.rpm 安裝yum源。 rpm -ivh mysql57-community-release-el6-8.noarch.rpm 清除緩存 yum clean all 下面幾個命令能夠順便學習一下 複製代碼 代碼以下:mysql

安裝

yum-config-manager 
yum install yum-utils -y 

if centos yum報錯Loaded plugins: fastestmirror

vim  /etc/yum/pluginconf.d/fastestmirror.conf 
enabled = 0

vim /etc/yum.conf
plugins=0

yum clean dbcache

禁用MySQL5.6的源

yum-config-manager --disable mysql56-community 
# 啓用MySQL5.7的源 
yum-config-manager --enable mysql57-community-dmr 
# 用下面的命令查看是否配置正確
yum repolist enabled | grep mysql
2、yum安裝MySQL5.7
本次安裝的版本是5.7.14,測試環境上原來有5.1.73版本,是升級安裝.
yum install mysql

3、初始化並啓動MySQL service mysqld start 直接啓動服務會報錯,以下: [root@localhost lib]# service mysqld start MySQL Daemon failed to start. 正在啓動 mysqld: [失敗] 解決方法:新版本要求先初始化mysql。 初始化mysql 複製代碼 代碼以下:linux

mysqld --initialize --user=mysql --datadir=/var/lib/mysql

或者 複製代碼 代碼以下:sql

mysqld --initialize-insecure --user=mysql --datadir=/var/lib/mysql

另外,當你是以mysql的帳戶登陸並執行程序的狀況下,你能夠將--user選項從命令中去掉。 經過官方文檔咱們能夠知道,若是我是root身份登陸Linux系 統,能夠執行:mysqld --initialize --user=mysql或者mysqld --initialize-insecure --user=mysql。若是我是以mysql用戶登陸Linux系統,能夠執行:mysqld --initialize或者mysqld --initialize-insecure。 無論在哪一個平臺上,使用--initialize選項就是以「默認安全模式」來進行安裝的(即包含一個隨機的root初始密碼的生成)。在這種狀況下,密 碼是被標誌爲過時的,你須要選擇一個新的密碼。使用--initialize-insecure選項,沒有root密碼生成;若是是這樣的話,在服務器投 入生產使用以前,你須要及時地爲帳戶指定密碼。 使用--initialize會爲root帳戶生成一個隨機的初始密碼,咱們可使用命令:mysql -u root -p,而後輸入密碼來登陸MySQL。使用--initialize-insecure不會爲root帳戶生成一個隨機的初始密碼,咱們可使用命 令:mysql -u root --skip-password直接登陸MySQL。 我初始化過程當中遇到報錯以下: [ERROR] --initialize specified but the data directory has files in it. Aborting. [ERROR] Aborting 解決方法: 複製代碼 代碼以下:數據庫

rm -rf /var/lib/mysql/* 緣由分析: mysqld服務會檢查數據目錄是否存在,以下: 若是數據目錄不存在,mysqld則會建立它。 若是數據目錄存在,且不是空目錄(即包含有文件或子目錄),mysqld會顯示一條錯誤信息並停止: [ERROR] --initialize specified but the data directory exists. Aborting. 遇到這種狀況,就將數據目錄刪除或重命名後,從新再試一次。 總之: 初始化以前,先檢查一下數據目錄是否存在,執行命令:ls -l /var/lib|grep mysql。如有,則執行命令: rm -rf /var/lib/mysql,將其刪除; 或者執行命令:mv /var/lib/mysql /var/lib/newname,將其重命名爲newname,就能夠了。 再次執行初始化命令,成功執行. [Note] A temporary password is generated for root@localhost: ihey0gFhTT;f 記住初始化生成的臨時的root密碼,登陸時會用到 初始化完成以後,直接啓動service mysqld start 4、鏈接MySQL並修改密碼 初始化時會生成一個臨時的root密碼,且登陸以後必須先改密碼,而後才能執行命令. 複製代碼 代碼以下:vim

mysql -uroot -p 臨時密碼
登陸以後,若是不當即改密碼,執行命令時報以下錯誤:
mysql> show databases ;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
修改密碼: set password=password('123[@Zxc](https://my.oschina.net/u/82595)');
或者:
複製代碼 代碼以下:

alter user 'root'@'localhost' identified by '123@Zxc';centos

再次執行成功. 從官方文檔中咱們瞭解到要用ALTER USER語句能夠修改密碼。登陸MySQL後,執行語句: 複製代碼 代碼以下:緩存

alter user 'root'@'localhost' identified by '123@Zxc';安全

就將root帳戶的密碼修改成123@Zxc了。 MySQL對用戶密碼安全性有所增強,因此設置的密碼必須包含有數字,大寫字母,小寫字母,特殊符號,若是你設置的密碼過於簡單,會提示: ERROR 1819 (HY000): Your password does NOT satisfy the CURRENT policy requirements。服務器

卸載mysql
yum remove mysql


yum remove mysql-libs 


 

使用yum安裝MySQL(CentOS 6.2):

 第一次先安裝repository

http://dev.mysql.com/doc/refman/5.6/en/linux-installation-yum-repo.html#yum-repo-setup

 

mkdir /home/tools

mkdir /home/tools/mysql

cd /home/tools/mysql

wget http://dev.mysql.com/get/mysql57-community-release-el6-8.noarch.rpm

sudo yum localinstall mysql57-community-release-el6-8.noarch.rpm

sudo yum install mysql-community-server

四、啓動

    4.一、啓動服務:

    service mysqld start 

    4.二、開機啓動

    chkconfig --levels 235 mysqld on

獲取mysql初始密碼
    grep 'temporary password' /var/log/mysqld.log

五、配置

 

在/etc/my.cnf中添加 

validate-password=OFF

否則簡單密碼將不能被mysql接受

    5.一、MySQL安全安裝

mysql -uroot -p 臨時密碼 登陸以後,若是不當即改密碼,執行命令時報以下錯誤: mysql> show databases ; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. 修改密碼: set password=password('123@Zxc'); 或者: 複製代碼 代碼以下:

若是mysql啓動失敗 ls -ld /var/run/mysqld/

chown mysql.mysql /var/run/mysqld/ [root@spark01 ~]# /etc/init.d/mysqld start

執行下面的命令:

    /usr/bin/mysql_secure_installation

    涉及內容包括:

    設置或更改root密碼 ( 初次安裝,初始密碼:1. ```
grep 'temporary password' /var/log/mysqld.log
```)

    移除匿名用戶

    禁止遠程登陸root

    刪除測試數據庫test

    從新加載受權表
set password=password('密碼要足夠複雜');
******** 完成上面的步驟,即成功完成安裝

 

常見異常



 
Mysql:is not allowed to connect to this MySQL server

若是你想鏈接你的mysql的時候發生這個錯誤:
ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL server

解決方法:
1。 改表法。多是你的賬號不容許從遠程登錄,只能在localhost。這個時候只要在localhost的那臺電腦,登入mysql後,更改 "mysql" 數據庫裏的 "user" 表裏的 "host" 項,從"localhost"改稱"%"

mysql -u root -p 

mysql>use mysql;

mysql>update user set host = '%' where user = 'root';

//刷新系統權限表
mysql>flush privileges;

mysql>select host, user from user;

2. 受權法。例如,你想myuser使用mypassword從任何主機鏈接到mysql服務器的話。
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
若是你想容許用戶myuser從ip爲192.168.1.3的主機鏈接到mysql服務器,並使用mypassword做爲密碼
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123ABCabc' WITH GRANT OPTION;

 
 

 

 重置密碼方法

 

 可嘗試這樣啓動  mysqld_safe -skip-grant-tables

 

或者

在/etc/my.ini的[mysqld]字段加入:
skip-grant-tables
重啓mysql服務,這時的mysql不須要密碼便可登陸數據庫
而後進入mysql
mysql>use mysql;
mysql>update user set password=password('新密碼') WHERE User='root';
mysql>flush privileges;
運行以後最後去掉my.ini中的skip-grant-tables,重啓mysqld便可。
5.7 中用:update user set authentication_string=password('123ABCabc') WHERE User='root';
ALTER USER USER() IDENTIFIED BY password('123ABCabc');
 
 
You must reset your password using ALTER USER statement before executing this statement

經過"skip-grant-tables"配置,客戶端鏈接 ,修改 user 表的字段 password_expired 爲  'N';

 


方法1: 用SET PASSWORD命令

  mysql -u root

  mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

方法2:用mysqladmin

  mysqladmin -u root password "newpass"

  若是root已經設置過密碼,採用以下方法

  mysqladmin -u root password oldpass "newpass"

方法3: 用UPDATE直接編輯user表

  mysql -u root

  mysql> use mysql;

  mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';

  mysql> FLUSH PRIVILEGES;

在丟失root密碼的時候,能夠這樣

  mysqld_safe --skip-grant-tables&

  mysql -u root mysql

  mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='root';

  mysql> FLUSH PRIVILEGES;

經驗內容僅供參考,若是您需解決具體問題(尤爲法律、醫學等領域),建議您詳


 

六、自定義MySQL數據文件目錄

    有時須要自定義MySQL的數據文件夾,即datadir;

    6.一、創建目標目錄,這裏假定爲 /home/data/mysql;    

 

mkdir /home/data

mv /var/lib/mysql /home/data/

    

 

    6.二、受權目錄讀寫權限給MySQL的運行用戶,上面安裝後的MySQL會以用戶mysql運行,則受權語句以下: 

chown -R         mysql:mysql /home/data/mysql

    6.三、修改/etc/my.cnf文件,[mysqld]增長datadir、socket配置,[client]也須要增長socket配置

            [mysqld]

            datadir=/home/data/mysql

            socket=/home/data/mysql/mysql.sock

            [client]

            socket=/home/data/mysql/mysql.sock

            

    6.四、若是啓動失敗,可經過cat /var/log/mysqld.log 查看啓動日誌,若是出現以下錯誤,則是因爲SELINUX限制引發

        InnoDB: Operating system error number 13 in a file operation.

        InnoDB: The error means mysqld does not have the access rights to

        InnoDB: the directory.

        解決方法: chcon -Rt mysqld_db_t /home/data/mysql

七、

1、Warning: World-writable config file '/etc/my.cnf' is ignored

To fix this problem, use the following command to change file’s permissions

要修改該問題,使用如下命令更改該文件的權限。

 

chmod 644 /etc/my.cnf

 四、找到my.cnf配置文件 
   若是/etc/目錄下沒有my.cnf配置文件,請到/usr/share/mysql/下找到*.cnf文件,拷貝其中一個到/etc/並更名爲my.cnf)中。命令以下: 
   [root@test1 mysql]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf

1.mysql -u root -p mysql # 第1個mysql是執行命令,第2個mysql是系統數據名稱

在mysql控制檯執行:

1.grant all privileges on . to 'root'@'%' identified by '123456' with grant option;

2.# root是用戶名,%表明任意主機,'123456'指定的登陸密碼(這個和本地的root密碼能夠設置不一樣的,互不影響)

3.flush privileges; # 重載系統權限

4.exit;

容許3306端口

1.iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

2.# 查看規則是否生效

3.iptables -L -n # 或者: service iptables status

4.# 此時生產環境是不安全的,遠程管理以後應該關閉端口,刪除以前添加的規則

5.iptables -D INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

PS,上面iptables添加/刪除規則都是臨時的,若是須要重啓後也生效,須要保存修改: service iptables save # 或者: /etc/init.d/iptables save

另外,

vi /etc/sysconfig/iptables # 加上下面這行規則也是能夠的

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

遠程管理數據庫的軟件,Windows系統下可使用Navicat,用了幾種遠程軟件,感受這個用起來蠻不錯的。

#先檢查是否安裝了iptables  
service iptables status  
#安裝iptables  
yum install -y iptables  
#升級iptables  
yum update iptables   
#安裝iptables-services  
yum install iptables-services  
#中止firewalld服務  
systemctl stop firewalld  
#禁用firewalld服務  
systemctl mask firewalld  



service iptables save  
systemctl restart iptables.service  


#註冊iptables服務  
#至關於之前的chkconfig iptables on  
systemctl enable iptables.service  
#開啓服務  
systemctl start iptables.service  
#查看狀態  
systemctl status iptables.service
相關文章
相關標籤/搜索