Centos7安裝Mysql-最方便、最快捷

 你想在Linux操做系統安裝Mysql?你不想去官網下載再複製?,那就來看看個人方案,簡單、快捷輕鬆安裝、使用。java

 首先,檢查安裝狀況

        1.查看有沒有安裝過:
 
        yum list installed MySQL* (有存在要卸載yum remove MySQL*)
 
        rpm -qa | grep mysql*
 
        2.查看有沒有安裝包:
 
        yum list mysql*
 

 而後,執行安裝 

[root@VM_235_40_centos ~]# yum install mysql Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package mariadb.x86_64 1:5.5.52-1.el7 will be installed --> Processing Dependency: mariadb-libs(x86-64) = 1:5.5.52-1.el7 for package: 1: mariadb-5.5.52-1.el7.x86_64 --> Running transaction check ---> Package mariadb-libs.x86_64 1:5.5.35-3.el7 will be updated ---> Package mariadb-libs.x86_64 1:5.5.52-1.el7 will be an update --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: mariadb x86_64 1:5.5.52-1.el7 os 8.7 M Updating for dependencies: mariadb-libs x86_64 1:5.5.52-1.el7 os 761 k Transaction Summary ================================================================================ Install 1 Package Upgrade ( 1 Dependent package) Total download size: 9.5 M Is this ok [y/d/N]: y Downloading packages: Delta RPMs disabled because /usr/bin/applydeltarpm not installed. (1/2): mariadb-libs-5.5.52-1.el7.x86_64.rpm | 761 kB 00:00 (2/2): mariadb-5.5.52-1.el7.x86_64.rpm | 8.7 MB 00:00 -------------------------------------------------------------------------------- Total 16 MB/s | 9.5 MB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Updating : 1:mariadb-libs-5.5.52-1.el7.x86_64 1/3 Installing : 1:mariadb-5.5.52-1.el7.x86_64 2/3 Cleanup : 1:mariadb-libs-5.5.35-3.el7.x86_64 3/3 Verifying : 1:mariadb-5.5.52-1.el7.x86_64 1/3 Verifying : 1:mariadb-libs-5.5.52-1.el7.x86_64 2/3 Verifying : 1:mariadb-libs-5.5.35-3.el7.x86_64 3/3 Installed: mariadb.x86_64 1:5.5.52-1.el7 Dependency Updated: mariadb-libs.x86_64 1:5.5.52-1.el7 Complete!node

  裝完以後,發現不是mysql,而是MariaDB,是mysql的一個分支,我要安裝的是mysql不是這個分支,只好刪除,先交給你們完全刪除Mysql語法:mysql

[root@localhost usr]# yum remove mysql mysql-server mysql-libs compat-mysql51  
[root@localhost usr]# rm -rf /var/lib/mysql  
[root@localhost usr]# rm /etc/my.cnf  

  若是裝了mysql-devel(其餘同樣add command),卸載爲:linux

[root@Tony_ts_tian init.d]# yum remove mysql mysql-devel mysql-server mysql-libs compat-mysql51 

  

Ok,卸載完成,從新安裝。sql

1.首先到官網下載組件,http://dev.mysql.com/downloads/repo/yum/,找到跟系統對應的版本(用uname查看版本,以下圖)數據庫

 

 

2.使用wget命令把相應的rpm包下下來,這個包很小隻有幾k

 [root@VM_235_40_centos ~]# wget https://repo.mysql.com//mysql57-community-release-el7-9.ch.rpm
    --2017-04-05 16:32:26--  https://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm
    Resolving repo.mysql.com (repo.mysql.com)... 23.50.25.213
    Connecting to repo.mysql.com (repo.mysql.com)|23.50.25.213|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 9224 (9.0K) [application/x-redhat-package-manager]
    Saving to: 'mysql57-community-release-el7-9.noarch.rpm'

    100%[============================================================>] 9,224       --.-K/s   in 0s

    2017-04-05 16:32:28 (75.1 MB/s) - 'mysql57-community-release-el7-9.noarch.rpm' saved [9224/9224]

  而後安裝上這個組件:vim

    [root@VM_235_40_centos ~]# rpm -Uvh mysql57-community-release-el7-9.noarch.rpm
    warning: mysql57-community-release-el7-9.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5KEY
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:mysql57-community-release-el7-9  ################################# [100%]

    [root@VM_235_40_centos ~]cd /etc/yum.repos.d

注:在安裝組件過程有些朋友平臺可能會出現異常:     Failed dependenciescentos

在安裝包後面加兩個參數,以下app

[root@magnesium Downloads]# rpm -ivhMySQL-server-5.5.29-2.linux2.6.x86_64.rpm --nodeps --force
Preparing...             ###########################################[100%]
   1:MySQL-server        ########################################### [100%]
......

  

安裝完成後會發現/etc/yum.repos.d會多了兩個mysql的repo文件:dom

    [root@VM_235_40_centos yum.repos.d]# ls 
    CentOS-Base.repo  mysql-community-source.repo
    CentOS-Epel.repo  mysql-community.repo

  

3. 安裝mysql

$ yum install mysql-server

根據步驟安裝就能夠了,不過安裝完成後,沒有密碼,須要重置密碼。

4. 重置密碼

重置密碼前,首先要登陸

$ mysql -u root

登陸時有可能報這樣的錯:ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2),緣由是/var/lib/mysql的訪問權限問題。下面的命令把/var/lib/mysql的擁有者改成當前用戶:

$ chown -R root:root /var/lib/mysql

而後,重啓服務:

$ service mysqld restart

接下來登陸重置密碼:

$ mysql -u root

mysql > use mysql;

mysql > update user set password=password(‘123456‘) where user=‘root‘;

mysql> flush privileges;

mysql > exit;

5. 開放3306端口

(1)執行:/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

 

或者在修改文件

$ sudo vim /etc/sysconfig/iptables

添加如下內容:

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

-A OUTPUT -p tcp -m tcp --dport 3306 -j ACCEPT

 

(2)保存後重啓防火牆:

service iptables save

service iptables restart

在本機登錄
mysql -u root
show databases;
use mysql;
select Host, User, Password from user;
+-----------------------+------+-------------------------------------------+
| Host | User | Password |
+-----------------------+------+-------------------------------------------+
| localhost | root | *18F54215F48E644FC4E0F05EC2D39F88D7244B1A |
| localhost.localdomain | root | |
| localhost.localdomain | | |
| localhost | | |
+-----------------------+------+-------------------------------------------+
能夠看到如上結果,只有localhost才設置了訪問的權限

 

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

flush privileges;

SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;

 

此時就能夠在其餘機器客戶端上鍊接該數據庫了

 

mysql -u root -p

show global variables like 'port';

 

設置表名忽略大小寫

/etc/my.cnf文件中[mysqld]的後面加
lower_case_table_names=1 
0,區分大小寫; 1,不區分

而後重啓數據庫便可

 

查看默認字符集

#mysql -u root - p
#(輸入密碼)
mysql> show variables like 'character_set%';

 

Mysql > show variables like 'collation_%';

利用mysql命令修改字符集:

Mysql > Set character_set_client = utf8;

Mysql > set character_set_connection = utf8;

Mysql > set character_set_database = utf8;

Mysql > set character_set_results = utf8;

Mysql > set character_set_server = utf8;

Mysql > set character_set_system = utf8;

相關文章
相關標籤/搜索