############################### Step 1:yum安装mysql服务器 #####################################

shell> yum install -y mysql-server php-mysql

Dependencies Resolved


================================================================

Package Arch Version Repository Size

================================================================

Installing:

mysql-server i686 5.1.69-1.el6_4 updates 8.8 M

php-mysql i686 5.3.3-23.el6_4 updates 78 k

Installing for dependencies:

mysql i686 5.1.69-1.el6_4 updates 917 k

mysql-libs i686 5.1.69-1.el6_4 updates 1.2 M

perl-DBD-MySQL i686 4.013-3.el6 base 134 k

perl-DBI i686 1.609-4.el6 base 705 k

php-common i686 5.3.3-23.el6_4 updates 525 k

php-pdo i686 5.3.3-23.el6_4 updates 74 k


Transaction Summary

================================================================

Install 8 Package(s)


Total download size: 12 M

Installed:

mysql-server.i686 0:5.1.69-1.el6_4 php-mysql.i686 0:5.3.3-23.el6_4


Dependency Installed:

mysql.i686 0:5.1.69-1.el6_4 mysql-libs.i686 0:5.1.69-1.el6_4 perl-DBD-MySQL.i686 0:4.013-3.el6 perl-DBI.i686 0:1.609-4.el6 php-common.i686 0:5.3.3-23.el6_4

php-pdo.i686 0:5.3.3-23.el6_4


Complete!


##################################### Step 2:启动mysqld服务 ########################################

shell> /etc/init.d/mysqld start

Initializing MySQL database: WARNING: The host ‘centos02′ could not be looked up with resolveip.

This probably means that your libc libraries are not 100 % compatible

with this binary MySQL version. The MySQL daemon, mysqld, should work

normally with the exception that host name resolving will not work.

This means that you should use IP addresses instead of hostnames

when specifying MySQL privileges !

Installing MySQL system tables…

OK

Filling help tables…

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 centos02 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 ]

shell>


shell> ls /var/lib/mysql/mysql.sock

/var/lib/mysql/mysql.sock

shell>

################################# 重置mysql密码 ##################################

### 按理说,刚装完mysql,root的默认密码应该为空。但不知道为什么,老是提示:

### ERROR 1045 (28000): Access denied for user root@localhost (using password: NO)

### Google 得真解。。。

Stop mysqld and restart it with the –skip-grant-tables option.

This enables anyone to connect without a password and with all privileges.

Because this is insecure, you might want to use –skip-grant-tables in conjunction

with –skip-networking to prevent remote clients from connecting.

# shell> mysqld_safe –skip-grant-tables –skip-networking &

Connect to the mysqld server with this command:

# shell> mysql

Issue the following statements in the mysql client. Replace the password with the password that you want to use.

# mysql> UPDATE mysql.user SET Password=PASSWORD(‘MyNewPass’)

# -> WHERE User=’root’;

# mysql> FLUSH PRIVILEGES;

The FLUSH statement tells the server to reload the grant tables into memory so that it notices the password change.


### 百度到网友的答案,没试过,记录一下:


关键点是:在使用skip-grant-tables参数的同时,还要加上skip-networking参数:


shell> mysqld_safe –skip-grant-tables –skip-networking &

接着使用SQL重置密码后,记得去掉skip-networking,以正常方式重启MySQL服务:


shell> /etc/init.d/mysqld restart

上面的方法需要重启两次服务,实际上还能更优雅一点,重启一次即可:


首先需要把用到的SQL语句保存到一个文本文件里(/path/to/init/file):


UPDATE mysql.user SET Password=PASSWORD(‘…’) WHERE User=’…’ AND Host= ‘…’;

FLUSH PRIVILEGES;

接着使用init-file参数启动MySQL服务,


shell> /etc/init.d/mysql stop

shell> mysqld_safe –init-file=/path/to/init/file &

此时,密码就已经重置了,最后别忘了删除文件内容,免得泄露密码。


############################## Step 3:安全配置 ########################################

shell> mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL

SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we’ll need the current

password for the root user. If you’ve just installed MySQL, and

you haven’t set the root password yet, the password will be blank,

so you should just press enter here.


Enter current password for root (enter for none):

OK, successfully used password, moving on…


Setting the root password ensures that nobody can log into the MySQL

root user without the proper authorisation.


You already have a root password set, so you can safely answer ‘n’.

############## 修改root密码 #######################

Change the root password? [Y/n] Y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

… Success!

By default, a MySQL installation has an anonymous user, allowing anyone

to log into MySQL without having to have a user account created for

them. This is intended only for testing, and to make the installation

go a bit smoother. You should remove them before moving into a

production environment.

################### 删除匿名用户 #######################

Remove anonymous users? [Y/n] Y

… Success!


Normally, root should only be allowed to connect from ‘localhost’. This

ensures that someone cannot guess at the root password from the network.

#################### 禁止root用户远程登陆 ################

Disallow root login remotely? [Y/n] Y

… Success!


By default, MySQL comes with a database named ‘test’ that anyone can

access. This is also intended only for testing, and should be removed

before moving into a production environment.

################## 删除测试用的test数据库 ###################

Remove test database and access to it? [Y/n] Y

– Dropping test database…

… Success!

– Removing privileges on test database…

… Success!


Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.


Reload privilege tables now? [Y/n] Y

… Success!


Cleaning up…




All done! If you’ve completed all of the above steps, your MySQL

installation should now be secure.


Thanks for using MySQL!

shell>


############################ Step 4:查看 ####################

shell> mysql -uroot -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 11

Server version: 5.1.69 Source distribution


Copyright (c) 2000, 2013, 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> SELECT user,host,password FROM mysql.user;

+——+———–+——————————————-+

| user | host | password |

+——+———–+——————————————-+

| root | localhost | *0521F2F8A6BCE7ACBF012F4C52DA8C5BE4113A05 |

| root | 127.0.0.1 | *0521F2F8A6BCE7ACBF012F4C52DA8C5BE4113A05 |

+——+———–+——————————————-+

2 rows in set (0.00 sec)


mysql> show databases;

+——————–+

| Database |

+——————–+

| information_schema |

| mysql |

+——————–+

2 rows in set (0.00 sec)


mysql> use information_schema

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> show tables

-> ;

+—————————————+

| Tables_in_information_schema |

+—————————————+

| CHARACTER_SETS |

| COLLATIONS |

| COLLATION_CHARACTER_SET_APPLICABILITY |

| COLUMNS |

| COLUMN_PRIVILEGES |

| ENGINES |

| EVENTS |

| FILES |

| GLOBAL_STATUS |

| GLOBAL_VARIABLES |

| KEY_COLUMN_USAGE |

| PARTITIONS |

| PLUGINS |

| PROCESSLIST |

| PROFILING |

| REFERENTIAL_CONSTRAINTS |

| ROUTINES |

| SCHEMATA |

| SCHEMA_PRIVILEGES |

| SESSION_STATUS |

| SESSION_VARIABLES |

| STATISTICS |

| TABLES |

| TABLE_CONSTRAINTS |

| TABLE_PRIVILEGES |

| TRIGGERS |

| USER_PRIVILEGES |

| VIEWS |

+—————————————+

28 rows in set (0.00 sec)


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> show tables

-> ;

+—————————+

| Tables_in_mysql |

+—————————+

| columns_priv |

| db |

| event |

| func |

| general_log |

| help_category |

| help_keyword |

| help_relation |

| help_topic |

| host |

| ndb_binlog_index |

| plugin |

| proc |

| procs_priv |

| servers |

| slow_log |

| tables_priv |

| time_zone |

| time_zone_leap_second |

| time_zone_name |

| time_zone_transition |

| time_zone_transition_type |

| user |

+—————————+

23 rows in set (0.00 sec)


mysql>exit