1、檢查MySQL及相關RPM包,是否安裝,若是有安裝,則移除(rpm –e 名稱)mysql
[root@localhost ~]# rpm -qa | grep -i mysqllinux
mysql-libs-5.1.66-2.el6_3.x86_64sql
[root@localhost ~]# yum -y remove mysql-libs*數據庫
2、下載Linux對應的RPM包,如:CentOS6.4_64對應的RPM包,以下:服務器
[root@localhost rpm]# lldom
total 74364編碼
-rw-r--r--. 1 root root 18442536 Dec 11 20:19 MySQL-client-5.6.15-1.el6.x86_64.rpmspa
-rw-r--r--. 1 root root 3340660 Dec 11 20:06 MySQL-devel-5.6.15-1.el6.x86_64.rpm.net
-rw-r--r--. 1 root root 54360600 Dec 11 20:03 MySQL-server-5.6.15-1.el6.x86_64.rpmserver
3、安裝MySQL
[root@localhost rpm]# rpm -ivh MySQL-server-5.6.15-1.el6.x86_64.rpm
[root@localhost rpm]# rpm -ivh MySQL-devel-5.6.15-1.el6.x86_64.rpm
[root@localhost rpm]# rpm -ivh MySQL-client-5.6.15-1.el6.x86_64.rpm
#修改配置文件位置
[root@localhost rpm]# cp /usr/share/mysql/my-default.cnf /etc/my.cnf
4、初始化MySQL及設置密碼
安裝mysql數據庫後,/usr/local/mysql/data下沒有數據,致使數據庫沒法啓動,因此須要初始化數據庫
執行如下腳本:
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/
註釋:該腳本爲perl腳本,須要有perl
該腳本將自動生成MySQL所須要
[root@localhost rpm]# service mysql start
[root@localhost rpm]# cat /root/.mysql_secret #查看root帳號密碼
# The random password set for the root user at Wed Dec 11 23:32:50 2013 (local time): qKTaFZnl
[root@localhost ~]# mysql -uroot –pqKTaFZnl
mysql> SET PASSWORD = PASSWORD('123456'); #設置密碼爲123456
mysql> exit
[root@localhost ~]# mysql -uroot -p123456
5、容許遠程登錄
mysql> use mysql;
mysql> select host,user,password from user;
+-----------------------+------+-------------------------------------------+
| host | user | password |
+-----------------------+------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost.localdomain | root | *1237E2CE819C427B0D8174456DD83C47480D37E8 |
| 127.0.0.1 | root | *1237E2CE819C427B0D8174456DD83C47480D37E8 |
| ::1 | root | *1237E2CE819C427B0D8174456DD83C47480D37E8 |
+-----------------------+------+-------------------------------------------+
mysql> update user set password=password('123456') where user='root';
mysql> update user set host='%' where user='root' and host='localhost';
mysql> flush privileges;
mysql> exit
6、設置開機自啓動
[root@localhost ~]# chkconfig mysql on
[root@localhost ~]# chkconfig --list | grep mysql
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
7、MySQL的默認安裝位置
/var/lib/mysql/ #數據庫目錄
/usr/share/mysql #配置文件目錄
/usr/bin #相關命令目錄
/etc/init.d/mysql #啓動腳本
8、修改字符集和數據存儲路徑
配置/etc/my.cnf文件,修改數據存放路徑、mysql.sock路徑以及默認編碼utf-8.
[client]
password = 123456
port = 3306
default-character-set=utf8
[mysqld]
port = 3306
character_set_server=utf8
character_set_client=utf8
collation-server=utf8_general_ci
# 注意linux下mysql安裝完後是默認:
表名區分大小寫,列名不區分大小寫; 0:區分大小寫,1:不區分大小寫
lower_case_table_names=1
#(設置最大鏈接數,默認爲 151,MySQL服務器容許的最大鏈接數16384; )
max_connections=1000
[mysql]
default-character-set = utf8
九、擴展
忘記密碼,跳過密碼認證登陸
一、關閉進程
Service mysql stop
二、執行如下命令跳過密碼認證
[root@maomao mysql]# mysqld_safe --skip-grant-tables &
[1] 28804
[root@maomao mysql]# 180424 02:29:27 mysqld_safe Logging to '/var/lib/mysql/maomao.err'.
180424 02:29:27 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
三、在另外一個窗口免密碼登陸
[root@maomao mysql]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.15 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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>
四、修改密碼
mysql> update mysql.user set password=PASSWORD('123456') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0