linux安装mysql,配置mysql文件

第一步安装mysql服务器软件。


请以root用户登录终端,运行以下命令:


1

[[email protected]]# yum -y install mysql-server ← 安装MySQL

如果想让yum安装的PHP程序支持mysql,请继续运行下面的命令添加PHP的mysql扩展,如果不使用PHP连接数据库可以忽略下面这外命令:


1

[[email protected]]# yum -y install php-mysql ← 安装php-mysql

第二步配置mysql。


打开mysql的配置文件/etc/my.cnf


1

[[email protected]] #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).

#在注释行的下面添加新的规则,让MySQL的默认编码为UTF-8

default-character-set = utf8  ← 添加这一行

#然后在配置文件的文尾填加如下语句:

[mysql]

default-character-set = utf8

第三步添加自启动。


1

[[email protected]]#chkconfig mysqld on ← 设置MySQL服务随系统启动自启动

检查启动状态:


1

2

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

mysqld   0:off  1:off  2:on   3:on   4:on   5:on   6:off

启动并初始化mysql服务


1

[[email protected]]#/etc/rc.d/init.d/mysqld start  ← 启动MySQL服务

[root@localhost ~]# service mysqld start

Initializing MySQL database:  Installing MySQL system tables...

141105  0:53:34 [Warning] '--default-character-set' is deprecated and will be re                                                                                                 moved in a future release. Please use '--character-set-server' instead.

OK

Filling help tables...

141105  0:53:34 [Warning] '--default-character-set' is deprecated and will be re                                                                                                 moved in a future release. Please use '--character-set-server' instead.

OK


To start mysqld at boot time you have to copy

support-files/mysql.server to the right place for your system


PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !

To do so, start the server, then issue the following commands:


/usr/bin/mysqladmin -u root password 'new-password'

/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'


Alternatively you can run:

/usr/bin/mysql_secure_installation


which will also give you the option of removing the test

databases and anonymous user created by default.  This is

strongly recommended for production servers.


See the manual for more instructions.


You can start the MySQL daemon with:

cd /usr ; /usr/bin/mysqld_safe &


You can test the MySQL daemon with mysql-test-run.pl

cd /usr/mysql-test ; perl mysql-test-run.pl


Please report any problems with the /usr/bin/mysqlbug script!


                                                          [  OK  ]

Starting mysqld:                                           [  OK  ]


第四步设置账号密码:


1

[[email protected]]#mysql -u root  ← 在没设置密码之时,用root用户登录MySQL服务器

1

mysql> select user,host,password from mysql.user;  ← 查看用户信息

mysql> select user,host,password from mysql.user;

+------+-----------------------+----------+

| user | host                  | password |

+------+-----------------------+----------+

| root | localhost             |          |

| root | localhost.localdomain |          |

| root | 127.0.0.1             |          |

|      | localhost             |          |

|      | localhost.localdomain |          |

+------+-----------------------+----------+

5 rows in set (0.00 sec)




设置root用户的密码


1

mysql> set password for [email protected]=password ('newpassword');

1

2

mysql> set password for [email protected]=password ('在这里填入root密码');

mysql> set password for [email protected]=password ('在这里填入root密码');

删除匿名登录账号:


1

mysql> delete from mysql.user where user=''; ← 删除匿名用户

1

mysql> flush privileges;  ← 刷新,使以上操作生效

退出mysql测试账号密码是否生效


1

mysql> exit;   ←退出MySQL服务器

第五步添加root远程登录账号并设置密码


1

2

3

4

5

6

[[email protected]]# mysql -u root -p; ← 通过密码用root登录

Enter password:    ← 在这里输入密码

   

mysql>CREATE USER 'root'@'%' IDENTIFIED BY '您的密码'; ← 增加root用户指定可以任意IP登录,如果想限制只能让指定IP登录请把%替换成IP地址

   

mysql>GRANT ALL PRIVILEGES ON * . * TO 'root'@'%' IDENTIFIED BY '你的密码' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ; ← 给新添加的root增加权限






















提示:ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'。前两天也出现过这个问题,网上找了一个比较流行的方法(见方法一),搞定了。今天又用这个试了试,却搞不定,在网上找了半天,终于发现是因为mysql数据库的user表里,存在用户名为空的账户即匿名账户,导致登录的时候是虽然用的是root,但实际是匿名登录的,通过错误提示里的''@'localhost'可以看出来,于是解决办法见方法二。


方法一:

1.关闭mysql

   # service mysqld stop

2.屏蔽权限

   # mysqld_safe --skip-grant-table

   屏幕出现: Starting demo from .....

3.新开起一个终端输入

   # mysql -u root mysql

   mysql> UPDATE user SET Password=PASSWORD('new') where USER='root';

   mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误

   mysql> \q


方法二:

1.关闭mysql

   # service mysqld stop

2.屏蔽权限

   # mysqld_safe --skip-grant-table

   屏幕出现: Starting demo from .....

3.新开起一个终端输入

   # mysql -u root mysql

   mysql> delete from user where USER='';

   mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误

   mysql> \q


你可能感兴趣的:(mysql,服务器,password,linux安装,用户登录)