MYsql安裝
- 安裝mysql前需要查詢系統(tǒng)中含有的有關(guān)mysql的軟件
rpm -qa | grep -i mysql //grep -i是不分大小寫字符查詢型雳,只要含有mysql就顯示
-
屏幕顯示:
image.png 下載mysql地址:http://dev.mysql.com/downloads/mysql/
用下面這兩個(gè)包
image.png
需要下載2個(gè)內(nèi)容当凡,一個(gè)是MySQL-5.6.21-1.el6.i686.rpm-bundle.tar山害,這個(gè)是幾個(gè)程序的合集包,另一個(gè)是MySQL-shared-compat-5.6.21-1.el6.i686.rpm沿量,這個(gè)是軟件包包括MySQL 3.23和MySQL 4.0的共享庫浪慌。如果你安裝了應(yīng)用程序動(dòng)態(tài)連接MySQL 3.23,但是你想要升級到y(tǒng)SQL 4.0而不想打破庫的從屬關(guān)系朴则,則安裝該軟件包而不要安裝MySQL-shared权纤。從MySQL 4.0.13起包含該安裝軟件包。
- 將2個(gè)文件上傳到redhat中乌妒,解壓MySQL-5.6.21-1.el6.i686.rpm-bundle.tar汹想。
tar xvf MySQL-5.6.21-1.el6.i686.rpm-bundle.tar
image.png
- 安裝MySQL-shared-compat替換mysql-libs,如果不替換撤蚊,在刪除mysql-libs欧宜,會(huì)提示postfix依賴于mysql-libs:
rpm -i MySQL-shared-compat-5.6.21-1.el6.i686.rpm
rpm -qa | grep -i mysql #如果有mysql-libs直接刪
#mysql-libs-5.1.71-1.el6.i686
#MySQL-shared-compat-5.6.21-1.el6.i686
yum remove mysql-libs
- 測試MySQL-server安裝,提示需要安裝perl:
rpm -ivh --test MySQL-server-5.6.21-1.el6.i686.rpm
yum install perl #如果你沒有perl的話需要安裝這個(gè)
- 安裝MySQL-server拴魄,MySQL-client:
rpm -ivh MySQL-server-5.6.21-1.el6.i686.rpm
Preparing... ########################################### [100%]
1:MySQL-server ########################################### [100%]
………………
………………
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.
You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.
Also, the account for the anonymous user has been removed.
In addition, you can run:
/usr/bin/mysql_secure_installation
………………
………………
# rpm -ivh MySQL-client-5.6.21-1.el6.i686.rpm
Preparing... ########################################### [100%]
1:MySQL-client ########################################### [100%]
下面這一段如果上面沒有彈這一堆說密碼的內(nèi)容,就說明他默認(rèn)給了你空密碼直接登錄就行席镀,如果出現(xiàn)了的話匹中,大意是全新安裝設(shè)置的root密碼在/root/.mysql_secret中,這是一個(gè)隨機(jī)密碼豪诲,你需要運(yùn)行/usr/bin/mysql_secure_installation顶捷,刪除anonymous用戶。當(dāng)然不建議用root用戶來運(yùn)行屎篱,rpm包已經(jīng)建了一個(gè)mysql用戶服赎,可以使用這個(gè)用戶:
more .mysql_secret
# The random password set for the root user at Tue Nov 18 22:57:46 2014 (local t
ime): NljqL63OYlGo5cqy <– 得到root訪問mysql的密碼:NljqL63OYlGo5cqy
service mysql start
#Starting MySQL... SUCCESS!
/usr/bin/mysql_secure_installation --user=mysql
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): <–使用剛才得到的root的密碼 NljqL63OYlGo5cqy
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] y <– 是否更換root用戶密碼,輸入y并回車交播,強(qiáng)烈建議更換
New password: <– 設(shè)置root用戶的密碼
Re-enter new password: <– 再輸入一次你設(shè)置的密碼
Password updated successfully!
Reloading privilege tables..
... Success!
##如果沒有上面那段提示一堆密碼內(nèi)容的話重虑,直接登錄進(jìn)去/usr/bin/mysql_secure_installation --user=mysql
就能修改密碼,改成你想要的秦士,然后下面繼續(xù)
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y <– 是否刪除匿名用戶,生產(chǎn)環(huán)境建議刪除缺厉,所以輸入y并回車
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y <–是否禁止root遠(yuǎn)程登錄,根據(jù)自己的需求選擇Y/n并回車,建議禁止
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y <– 是否刪除test數(shù)據(jù)庫,輸入y并回車
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y 是否重新加載權(quán)限表,輸入y并回車
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...
- 至此隧土,MySQL已經(jīng)安裝完成提针,最后看一下是否已將MySQL加到開機(jī)服務(wù)里:
# chkconfig
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iscsi 0:off 1:off 2:off 3:on 4:on 5:on 6:off
iscsid 0:off 1:off 2:off 3:on 4:on 5:on 6:off
lvm2-monitor 0:off 1:on 2:on 3:on 4:on 5:on 6:off
mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off
multipathd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off <-看到這個(gè)OK了
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off
restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off
數(shù)據(jù)庫安裝好以后修改遠(yuǎn)程登錄狀態(tài)
mysql>use mysql;
mysql> select host from user where user = 'root';
+-----------+
| host |
+-----------+
| 127.0.0.1 |
| localhost |
+-----------+
2 rows in set (0.00 sec)
- 出現(xiàn)情況如上圖
Root用戶host值是上圖兩個(gè),都是本地曹傀,要修改為%
mysql>select host from user where user = 'root';
+-----------+
| host |
+-----------+
| % |
| 127.0.0.1 |
+-----------+
2 rows in set (0.00 sec)
mysql>flush privileges; ##讓改動(dòng)生效
mysql> quit
data目錄遷移
默認(rèn)mysql是安裝在/var/lib/mysql目錄下辐脖,但是一般只有15G空間,令人至瘋皆愉,所以裝完必須遷移
1嗜价、home目錄下建立data目錄
cd /homemkdir data
2艇抠、把MySQL服務(wù)進(jìn)程停掉:
mysqladmin -u root -p shutdown
3、把/var/lib/mysql整個(gè)目錄移到/home/data
mv /var/lib/mysql /home/data/
這樣就把MySQL的數(shù)據(jù)文件移動(dòng)到了/home/data/mysql下
4炭剪、找到my.cnf配置文件
如果/etc/目錄下沒有my.cnf配置文件练链,請到/usr/share/mysql/下找到*.cnf文件,拷貝其中一個(gè)到/etc/并改名為my.cnf)中奴拦。命令如下:
[root@test1 mysql]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
5媒鼓、編輯MySQL的配置文件/etc/my.cnf
為保證MySQL能夠正常工作,需要指明mysql.sock文件的產(chǎn)生位置错妖。修改socket=/var/lib/mysql/mysql.sock一行中等號右邊的值為:/home/mysql/mysql.sock 绿鸣。操作如下:
vi my.cnf
(用vi工具編輯my.cnf文件,找到下列數(shù)據(jù)修改之)# The MySQL server[mysqld] port = 3306#socket = /var/lib/mysql/mysql.sock(原內(nèi)容暂氯,為了更穩(wěn)妥用“#”注釋此行)socket = /home/data/mysql/mysql.sock〕蹦!(加上此行)
6、修改MySQL啟動(dòng)腳本/etc/init.d/mysql
最后痴施,需要修改MySQL啟動(dòng)腳本/etc/init.d/mysqld擎厢,把其中datadir=/var/lib/mysql一行中,等號右邊的路徑改成你現(xiàn)在的實(shí)際存放路徑:home/data/mysql辣吃。
[root@test1 etc]# vi /etc/init.d/mysql#datadir=/var/lib/mysql(注釋此行)datadir=/home/data/mysql (加上此行)
如果是CentOS還要改 /usr/bin/mysqld_safe 相關(guān)文件位置动遭;
最后 做一個(gè)mysql.sock 鏈接:
in -s /home/data/mysql/mysql.sock /var/lib/mysql/mysql.sock
7、重新啟動(dòng)MySQL服務(wù)
/etc/init.d/mysql start或者service mysql restart
8神得、修改整個(gè)目錄權(quán)限
chown -R mysql:mysql /home/data/mysql/
chmod -R 777 /home/data/mysql/*
無密碼登錄問題
- 如果環(huán)境出現(xiàn)登錄時(shí)候報(bào)錯(cuò)
“ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)”厘惦。
是密碼不對
1.首先修改/etc/my.cnf中的mysqld,添加skip-grant-tables哩簿,保存退出
image.png
2宵蕉,執(zhí)行 service mysql restart,重啟之后节榜,再嘗試登陸mysql羡玛,遇到需要輸入密碼,直接回車
3宗苍,執(zhí)行 use mysql
4缝左,執(zhí)行 update user set password=PASSWORD("你的新密碼") where user="root"; 這是mysql的執(zhí)行語句,所以記得末尾加上;號
5浓若,執(zhí)行 exit 渺杉,退出mysql
6,執(zhí)行 vim /etc/mysql/my.cnf挪钓,打開文件后是越, 把剛才添加的 skip-grant-tables 刪掉,保存退出
7碌上,執(zhí)行 service mysql restart倚评。