安装mysql

1、检测是否已安装mysql

[root@localhost bin]# rpm -qa | grep mysql

mysql-libs-5.1.71-1.el6.i686

[root@localhost bin]

2、安装mysql和mysql-server

[root@localhost bin]# yum install -y mysql

[root@localhost bin]# yum install -y mysql-server

3、启动mysql,注意这里的服务名字是“mysqld”

[root@localhost bin]# service mysqld start

shell-init: error retrieving current directory: getcwd: cannot access parent directories: 没有那个文件或目录

chdir: error retrieving current directory: getcwd: cannot access parent directories: 没有那个文件或目录

初始化 MySQL 数据库: Installing MySQL system tables...

OK

Filling help tables...

OK                                                         [确定]

正在启动 mysqld                                          [确定]

[root@localhost bin]# 

4、配置MySQL开机启动

[root@localhost bin]# chkconfig mysqld on

[root@localhost bin]# chkconfig --list mysqld

mysqld         0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭

[root@localhost bin]# 

5、设置root用户密码 ,设置密码的命令是“mysqladmin -u 用户名password密码

[root@localhost bin]# mysqladmin -u root password 543092   //密码是543092

6、修改密码,命令是“mysqladmin -u 用户名-p password 新密码

[root@localhost bin]# mysqladmin -u root -p password 123456     //新密码123456

Enter password:        //这里要求输入旧密码

[root@localhost bin]# 

7、mysql配置文件“/etc/my.cnf”

[root@localhost bin]# cat /etc/my.cnf 

[mysqld]

datadir=/var/lib/mysql            //数据库文件目录

socket=/var/lib/mysql/mysql.sock    //socket文件

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

 

[mysqld_safe]

log-error=/var/log/mysqld.log     //日志文件

pid-file=/var/run/mysqld/mysqld.pid    //进程pid文件

 

8、修改端口

1)mysql默认的端口是3306,可以使用netstat来查看

[root@localhost bin]# netstat -tunlp | grep mysqld

tcp        0      0 0.0.0.0:3306               0.0.0.0:*                  LISTEN      4339/mysqld         

2)端口在配置文件中修改,打开配置文件,在后面追加一句话”port=xxx”

[root@localhost bin]# gedit /etc/my.cnf

port=3307      

3)重启myslqd服务,查看端口

[root@localhost bin]# service mysqld restart

shell-init: error retrieving current directory: getcwd: cannot access parent directories: 没有那个文件或目录

chdir: error retrieving current directory: getcwd: cannot access parent directories: 没有那个文件或目录

^[[A停止mysqld                                          [确定]

正在启动mysqld                                          [确定]

[root@localhost bin]# netstat -tunlp | grep mysqld

tcp        0      0 0.0.0.0:3307               0.0.0.0:*                   LISTEN      4604/mysqld              

05-11 11:22