Linux下安装Mysql,使用yum来安装,方便简单,按步骤操作就可以了。
yum安装
查看是否已经安装mysql 如果已经安装了mysql 按步骤删除
[root@hadoop001 ~]# yum list installed | grep mysql [root@hadoop001 ~]# rpm -qa | grep mysql* [root@hadoop001 ~]# yum remove mysql mysql-server mysql-libs compat-mysql51 -y
查看/var/lib/mysql文件和/etc/my.cnf 存在的话删除
[root@hadoop001 ~]# rm -rf /var/lib/mysql [root@hadoop001 ~]# rm /etc/my.cnf
查看是否已经删除mysql安装 如有存在的话 则继续删除
[root@hadoop001 ~]# yum list installed | grep mysql* [root@hadoop001 ~]# rpm -qa | grep mysql*
查看是否存在mysql-server和mysql-devel
[root@hadoop001 ~]# yum list mysql*
存在的话 直接安装,先安装mysql客户端
[root@hadoop001 ~]# yum install mysql -y
安装mysql服务端
[root@hadoop001 ~]# yum install mysql-server mysql-devel -y Installed: mysql-devel.x86_64 0:5.1.71-1.el6 mysql-server.x86_64 0:5.1.71-1.el6 Dependency Installed: keyutils-libs-devel.x86_64 0:1.4-4.el6 krb5-devel.x86_64 0:1.10.3-10.el6_4.6 libcom_err-devel.x86_64 0:1.41.12-18.el6 libselinux-devel.x86_64 0:2.0.94-5.3.el6_4.1 libsepol-devel.x86_64 0:2.0.41-4.el6 openssl-devel.x86_64 0:1.0.1e-15.el6 perl-DBD-MySQL.x86_64 0:4.013-3.el6 perl-DBI.x86_64 0:1.609-4.el6 zlib-devel.x86_64 0:1.2.3-29.el6 Complete!
如果出现上面的的提示,则说明mysql已经安装成功了,下面进行mysql的配置和用户的添加。
[root@hadoop001 ~]# vim /etc/my.cnf 添加如下信息在mysqld中, #设置字符集编码格式 character-set-server=utf8 #忽略表英文字母大小写 lower_case_table_names=1
查看mysql状态,并启动mysql服务,并设置为开机启动项
[root@hadoop001 ~]# service mysqld status [root@hadoop001 ~]# service mysqld start [root@hadoop001 ~]# chkconfig mysqld on
添加用户
第一次登录mysql数据,不需要输入密码
- 该表添加访问用户
[root@hadoop001 ~]# mysql -uroot
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select host,user,password from user;
+-----------+------+----------+
| host | user | password |
+-----------+------+----------+
| localhost | root | |
| hadoop001 | root | |
| 127.0.0.1 | root | |
| localhost | | |
| hadoop001 | | |
+-----------+------+----------+
5 rows in set (0.00 sec)
mysql> update user set password=password("123456") where user="root";
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> update user set host = '%' where user = 'root' and host='localhost';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host | user | password |
+-----------+------+-------------------------------------------+
| % | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| hadoop001 | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| 127.0.0.1 | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | | |
| hadoop001 | | |
+-----------+------+-------------------------------------------+
5 rows in set (0.00 sec)
mysql> flush privileges ;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
- 授权添加访问用户
忘记密码时,如何操作?
注:
我们启动mysql的时候,启动失败:
[root@hadoop001 mysql]# service mysqld restart Stopping mysqld: [ OK ] MySQL Daemon failed to start. Starting mysqld: [FAILED] 查看mysql日志信息 [root@hadoop001 log]# less /var/log/mysqld.log 发现报了如下错误 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 通过口令初始化mysql数据库,然后在重新启动 [root@hadoop001 log]# mysql_install_db [root@hadoop001 log]# service mysqld start