redhat6.4下使用centos的yum源安装mysql
思路:卸载redhat自带yum,然后下载centos的yum,安装后修改配置文件
1、首先到http://mirrors.163.com/centos下载软件包
x86 地址:http://mirrors.163.com/centos/6/os/i386/Packages/
x86_64 地址:http://mirrors.163.com/centos/6/os/x86_64/Packages/
必要下载的软件包有(以64位系统为例):
a python-iniparse-0.3.1-2.1.el6.noarch.rpm
b yum-3.2.29-40.el6.centos.noarch.rpm
c yum-metadata-parser-1.1.2-16.el6.x86_64.rpm
d yum-plugin-fastestmirror-1.1.30-14.el6.noarch.rpm
下载命令:
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了。
设置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;
MySQL新建用户,授权,删除用户,修改密码
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 [email protected] 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 [email protected] identified by ‘123‘;
给来自10.163.225.87的用户joe分配可对数据库vtdc的employee表进行select,insert,update,delete,create,drop等操作的权限,并设定口令为123。
mysql>grant all privileges on vtdc.* to [email protected] identified by ‘123‘;
给来自10.163.225.87的用户joe分配可对数据库vtdc所有表进行所有操作的权限,并设定口令为123。
mysql>grant all privileges on *.* to [email protected] 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‘;
二、数据库表
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;//把字段修改到指定字段之后
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。