思路:卸載redhat自帶yum,而後下載centos的yum,安裝後修改配置文件php
1、首先到http://mirrors.163.com/centos下載軟件包html
x86 地址:http://mirrors.163.com/centos/6/os/i386/Packages/node
x86_64 地址:http://mirrors.163.com/centos/6/os/x86_64/Packages/python
必要下載的軟件包有(以64位系統爲例):mysql
a python-iniparse-0.3.1-2.1.el6.noarch.rpmsql
b yum-3.2.29-40.el6.centos.noarch.rpm數據庫
c yum-metadata-parser-1.1.2-16.el6.x86_64.rpmvim
d yum-plugin-fastestmirror-1.1.30-14.el6.noarch.rpmcentos
下載命令:緩存
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/python-iniparse-0.3.1-2.1.el6.noarch.rpm wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-metadata-parser-1.1.2-16.el6.x86_64.rpm wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-3.2.29-40.el6.centos.noarch.rpm wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.30-14.el6.noarch.rpm |
[注] :版本不必定要最新的,若是出現404找不到文件,能夠經過http://mirrors.163.com/centos/6/os/x86_64/Packages/查看版本號,並進行微調
2、卸載RedHat自帶的yum
rpm -qa | grep yum | xargs rpm -e --nodeps
注:a、xargs是一條Unix和類Unix操做系統的經常使用命令。它的做用是將參數列表轉換成小塊分段傳遞給其餘命令,以免參數列表過長的問題
b、--nodeps 強制卸載,無論依賴性
3、安裝下載的centos的yum包:
rpm -ivh python-iniparse-0.3.1-2.1.el6.noarch.rpm
rpm -ivh yum-metadata-parser-1.1.2-16.el6.x86_64.rpm
rpm -ivh yum-3.2.29-40.el6.centos.noarch.rpm yum-plugin-fastestmirror-1.1.30-14.el6.noarch.rpm
[注] :最後2個須要一塊兒安裝,不然會出現依賴性錯誤
4、到http://mirrors.163.com的centos幫助文檔中下載CentOS6-Base-163.repo文件,存放到/etc/yum.repo.d中
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
5、編輯CentOS6-Base-163.repo文件,將其中的$releasever更改成centos的版本號
下面是修改好的:
# CentOS-Base.repo # # The mirror system uses the connecting IP address of the client and the # update status of each mirror to pick mirrors that are updated to and # geographically close to the client. You should use this for CentOS updates # unless you are manually picking other mirrors. # # If the mirrorlist= does not work for you, as a fall back you can try the # remarked out baseurl= line instead. # #
[base] name=CentOS-6 - Base - 163.com baseurl=http://mirrors.163.com/centos/6/os/$basearch/ #mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=os gpgcheck=1 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
#released updates [updates] name=CentOS-6 - Updates - 163.com baseurl=http://mirrors.163.com/centos/6/updates/$basearch/ #mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=updates gpgcheck=1 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
#additional packages that may be useful [extras] name=CentOS-6 - Extras - 163.com baseurl=http://mirrors.163.com/centos/6/extras/$basearch/ #mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=extras gpgcheck=1 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
#additional packages that extend functionality of existing packages [centosplus] name=CentOS-6 - Plus - 163.com baseurl=http://mirrors.163.com/centos/6/centosplus/$basearch/ #mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=centosplus gpgcheck=1 enabled=0 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
#contrib - packages by Centos Users [contrib] name=CentOS-6 - Contrib - 163.com baseurl=http://mirrors.163.com/centos/6/contrib/$basearch/ #mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=contrib gpgcheck=1 enabled=0 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6 |
6、yum clean all清除原有緩存
7、yum makecache 獲取yum列表
出現下面提示,表示yum更改完成:
Metadata Cache Created
8、yum 安裝mysql
安裝MySQL。
[root@sample ~]# yum -y install mysql-server ← 安裝MySQL
[root@sample ~]# yum -y install php-mysql ← 安裝php-mysql
配置MySQL
[root@sample ~]#vim /etc/my.cnf ← 編輯MySQL的配置文件
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1 ← 找到這一行,在這一行的下面添加新的規則,讓MySQL的默認編碼爲UTF-8
default-character-set = utf8 ← 添加這一行
而後在配置文件的文尾填加以下語句:
[mysql]
default-character-set = utf8
啓動MySQL服務
[root@sample ~]# chkconfig mysqld on ← 設置MySQL服務隨系統啓動自啓動
[root@sample ~]# chkconfig --list mysqld ← 確認MySQL自啓動
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off ← 若是2--5爲on的狀態就OK
[root@sample ~]#/etc/rc.d/init.d/mysqld start ← 啓動MySQL服務
Initializing MySQL database: [ OK ]
Starting MySQL: [ OK ]
MySQL初始環境設定
[1]爲MySQL的root用戶設置密碼
MySQL在剛剛被安裝的時候,它的root用戶是沒有被設置密碼的。首先來設置MySQL的root密碼。
[root@sample ~]# mysql -u root ← 用root用戶登陸MySQL服務器
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 4.1.20
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>grant all on mysql.* to 'root'@'localhost' identified by 'password';
以後 就能夠經過密碼登陸mysql了。
1.修改配置文件
sudo vim /etc/mysql/my.cnf
把bind-address參數的值改爲你的內/外網IP或0.0.0.0,或者直接註釋掉這行.
2.登陸數據庫
mysql -u root -p
輸入密碼
mysql> use mysql;
3.查詢host
mysql> select user,host from user;
4.建立host
若是沒有"%"這個host值,就執行下面這兩句:
mysql> update user set host='%' where user='root';
mysql> flush privileges;
5.受權用戶
任意主機以用戶root和密碼mypwd鏈接到mysql服務器
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypwd' WITH GRANT OPTION;
mysql> flush privileges;
IP爲192.168.1.102的主機以用戶myuser和密碼mypwd鏈接到mysql服務器
mysql> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.102' IDENTIFIED BY 'mypwd' WITH GRANT OPTION;
mysql> flush privileges;
1. CREATE USER
語法:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
例子: CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'pig'@'192.168.1.101_' IDENDIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '';
CREATE USER 'pig'@'%';
實例1:
mysql> create user jss;
這樣建立的用戶,能夠從任意安裝了mysql客戶端,並可以訪問目標服務器的機器上建立鏈接,無須密碼.例如,從ip:10.0.0.99的客戶端執行鏈接:
mysql -ujss -h 172.16.1.110
查看該用戶:
mysql> select user,host,password from user where user='jss';
SELECT USER(); //顯示當前用戶
實例2:
mysql> create user jss_ps identified by 'jss';
用戶鏈接時,必須指定密碼,那就能夠在建立用戶時,經過指定identified by子句來設定密碼
用密碼登錄:
mysql -ujss_ps -p -h 172.16.1.110
若是但願指定的用戶只能從某臺指定的域(domain)或主機訪問,能夠在建立用戶時指定host,例如,指定用戶只能從10.0.0.99訪問
mysql> create user jss_ip@10.0.0.99 identified by password '123456';
2. 使用GRANT語句
語法:mysql> grant 權限1,權限2,...權限n on 數據庫名稱.表名稱 to 用戶名@用戶地址 identified by '鏈接口令';
權限1,權限2,...權限n表明
select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14個權限
實例:
mysql>grant select,insert,update,delete,create,drop on vtdc.employee to joe@10.163.225.87 identified by '123';
給來自10.163.225.87的用戶joe分配可對數據庫vtdc的employee表進行select,insert,update,delete,create,drop等操做的權限,並設定口令爲123。
mysql>grant all privileges on vtdc.* to joe@10.163.225.87 identified by '123';
給來自10.163.225.87的用戶joe分配可對數據庫vtdc全部表進行全部操做的權限,並設定口令爲123。
mysql>grant all privileges on *.* to joe@10.163.225.87 identified by '123';
給來自10.163.225.87的用戶joe分配可對全部數據庫的全部表進行全部操做的權限,並設定口令爲123。
mysql>grant all privileges on *.* to joe@localhost identified by '123';
給本機用戶joe分配可對全部數據庫的全部表進行全部操做的權限,並設定口令爲123。
3. 直接向mysql.user表插入記錄:
mysql> insert into user (host,user,password) values ('%','jss_insert',password('jss'));
mysql>flush privileges; //刷新系統權限表
4. 修改mysql用戶密碼方式:
a. 使用mysqladmin語法:mysqladmin -u用戶名 -p舊密碼 password 新密碼
例如:mysqladmin -u root -p 123 password 456;
b. 直接修改user表的用戶口令:
語法:update mysql.user set password=password('新密碼') where User="phplamp" and Host="localhost";
實例:update user set password=password('54netseek') where user='root';
flush privileges;
c. 使用SET PASSWORD語句修改密碼:語法:
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
若是是當前登錄用戶用SET PASSWORD = PASSWORD("newpassword");
實例:
set password for root@localhost=password('');
SET PASSWORD FOR name=PASSWORD('new password');
SET PASSWORD FOR 'pig'@'%' = PASSWORD("123456");
5. 刪除用戶和撤銷權限:
a. 取消一個帳戶和其權限
Drop USER user;
drop user username@'%'
drop user username@localhost
b. 取消受權用戶:
語法:REVOKE privilege ON databasename.tablename FROM 'username'@'host';
例子: REVOKE SELECT ON *.* FROM 'pig'@'%';
REVOKE SELECT ON test.user FROM 'pig'@'%';
revoke all on *.* from sss@localhost ;
revoke all on user.* from 'admin'@'%';
SHOW GRANTS FOR 'pig'@'%'; //查看受權
c. 刪除用戶:
語法: Delete from user where user = "user_name" and host = "host_name" ;
例子:delete from user where user='sss' and host='localhost';
2、數據庫表
1.查看全部數據庫: 數據庫目錄:/usr/local/mysql/data
mysql> SHOW DATABASES; //顯示數據庫
mysql> USE abccs //進入數據庫
mysql> SHOW TABLES; //顯示錶
mysql> DESCRIBE mytable; //顯示錶結構
mysql> CREATE DATABASE abccs; //建立一個數據庫
mysql> CREATE TABLE mytable (name VARCHAR(20), sex CHAR(1), birth DATE, birthaddr VARCHAR(20)); //建立表
mysql> insert into mytable values (‘abccs’,‘f’,‘1977-07-07’,‘china’); //插入表數據
使用文本方式插入數據:
{
mysql.txt內容:abccs f 1977-07-07 china
mary f 1978-12-12 usa
tom m 1970-09-02 usa
mysql> LOAD DATA LOCAL INFILE "mytable.txt" INTO TABLE pet; //導入TXT文件數據
}
2.刪除數據庫:
mysql> drop database drop_database; //刪除一個已經肯定存在的數據庫
alter table 表名 ENGINE=存儲引擎名; //修改表的存儲引擎
alter table 表名 drop 屬性名; //刪除字段
alter table 舊錶名 rename to 新表名; //修改表名
alter table 表名 modify 屬性名 數據類型; //修改字段數據類型
alter table 表名 change 舊屬性名 新屬性名 新數據類型; //修改字段名
alter table 表名 drop FOREING KEY 外鍵別名; //刪除子表外鍵約束
增長表字段:
{ alter table example add phone VACGAR(20); //增長無約束的字段
alter table example add age INT(4) NOT NULL; //增長萬增約束的字段
alter table example add num INT(8) PRIMARY KEY FIRST; //表的第一個位置增長字段
alter table example add address VARCHAR(30) NOT NULL AFTER phone; //表的指定位置以後增長字段
alter table example modify name VARCHAR(20) FIRST; //把字段修改到第一位
alter table example modify num INT(8) ATER phone;//把字段修改到指定字段以後
}