centos6.2 yum安装mysql数据库

发布时间:2020-08-14编辑:脚本学堂
本文介绍了centos6.2系统中yum安装mysql数据库的方法,mysql初始化环境设置方法,需要的朋友参考下。

一,yum安装mysql,命令:
 

[root@mysql ~]# yum -y install mysql-server //自动从网上搜索资源并自动下载安装
[root@mysql ~]# chkconfig mysqld on    //设置开机启动MySql服务

检查是否为开机启动:
打入命令:
 

[root@mysql ~]# chkconfig –list

看到:mysqld0:off   1:off   2:on    3:on    4:on    5:on    6:off 即表示已设置为开机启动,2、3、4、5为on

二,启动MySql服务
 

/etc/rc.d/init.d/mysqld start   //从文件启动MySql服务
service mysqld start//以服务名方式启动

三,mysql初始化环境设置
设置MySQL的root用户设置密码,因为MySQL被安装时,它的root用户时没有设置密码的。
 

[root@mysql ~]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.1.61 Source distribution
Copyright (c) 2000, 2011, 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数据库用户信息

四,mysql初始化环境设置第二部分
打入命令:

select user,host,password from mysql.user; //可以看到密码都为空
– +------+-----------+----------+
– | user | host | password |
– +------+-----------+----------+
– | root | localhost ||
– | root | mysql||
– | root | 127.0.0.1 ||
– | | localhost ||
– | | mysql||
– +------+-----------+----------+
– 5 rows in set (0.00 sec)
– set password for root@localhost=password(‘123456789’); //设置root用户密码为123456789
 

五,mysql初始化环境设置 第三部分
 打入命令:

select user,host,password from mysql.user; //查看刚设置的密码,可以看到密码已经经过加密处理
– +------+-----------+-------------------------------------------+
– | user | host | password    |
– +------+-----------+-------------------------------------------+
– | root | localhost | *CC67043C7BCFF5EEA5566BD9B1F3C74FD9A5CF5D |
– | root | mysql|   |
– | root | 127.0.0.1 |   |
– | | localhost |   |
– | | mysql|   |
– +------+-----------+-------------------------------------------+
– 5 rows in set (0.00 sec)
 

 打入exit命令退出数据库连接,测试root密码是否生效
 
六,mysql初始化环境设置 第四部分
 打入命令:mysql –u root
 

– ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql -u root' at line 1
– 弹出图上提示说明刚设置的密码已生效
 下面通过密码登陆,打入命令:mysql –u root –p //在Enter password后输入刚才设置的密码,看到下面即表示登陆成功
– [root@mysql ~]# mysql -u root -p
– Enter password:
– Welcome to the MySQL monitor. Commands end with ; or g.
– Your MySQL connection id is 3
– Server version: 5.1.61 Source distribution
– Copyright (c) 2000, 2011, 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>
 

 
七,增删改查语句
 

 Show databases; //查看系统已存在的数据库
 use databasesname;   //选择需要使用的数据库
 drop database databasename; //删除选定的数据库
 exit    //退出数据库的连接
 create database test01;    //建立名为test的数据库
– mysql> show databases;
– +--------------------+
– | Database |
– +--------------------+
– | information_schema |
– | mysql    |
– | test|
– | test01   |
– +--------------------+
– 4 rows in set (0.00 sec)
 

可以看到新建的test01数据库
 

 use test01; //连接到数据库test01
– mysql> use test01;
– Database changed
 mysql> create talbe test(num int,name varchar(50)); //在数据库中建立表
– mysql> create table test(num int,name varchar(50));
– Query OK, 0 rows affected (0.11 sec)
 mysql> insert into test values(1,’Hello World!’) ;   //插入一个值到表中
– mysql> create table test(num int,name varchar(50));
– Query OK, 0 rows affected (0.11 sec)
 select * from test;   //查看数据库中表的信息
–  mysql> select * from test;
– +------+--------------+
– | num | name    |
– +------+--------------+
– |    1 | Hello World! |
– +------+--------------+
– 1 row in set (0.00 sec)