CentOS+MySQL 相约日记

12 May 2013

MySQL 我的最爱

Step1 安装

1 $ sudo yum install mysql mysql-server

Step2 配置

1 $ sudo vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 设置编码格式
default-character-set=utf8
# 设置不区分大小写
lower_case_table_names=1 

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysql]
# 设置编码格式
default-character-set=utf8

Step3 启动服务

1 # 设置MySQL服务随系统启动自启动
2 $ sudo chkconfig mysqld on
3 # 确认MySQL自启动,如果2--5为on的状态就OK
4 $ sudo chkconfig --list mysqld
5 mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
6 # 启动MySQL服务
7 $ sudo /etc/rc.d/init.d/mysqld start
8 Initializing MySQL database:			[ OK ]
9 Starting MySQL:				[ OK ]

Step4 环境设定

为 MySQL 的 root 用户设置密码,MySQL 在刚刚被安装的时候,它的 root 用户是没有被设置密码的。

1 $ 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): <-- 输入系统 root 密码
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] <-- ENTER
New password: <-- 你的 MySQL root 密码
Re-enter new password: <-- 你的 MySQL root 密码
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.

# 删除匿名用户,在MySQL刚刚被安装后,存在用户名、密码为空的用户。这使得数据库
服务器有无需密码被登录的可能性。为消除隐患,将匿名用户删除。
Remove anonymous users? [Y/n] <-- ENTER 
... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] <-- ENTER
... 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] <-- ENTER
- 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] <-- ENTER
... 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!

Step5 测试

 1 # 用 root 用户通过密码登录
 2 $ mysql -u root -p
 3 Enter password: <-- 输入密码
 4 Welcome to the MySQL monitor.  Commands end with ; or \g.
 5 Your MySQL connection id is 50
 6 Server version: 5.1.69 Source distribution
 7 
 8 Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 9 
10 Oracle is a registered trademark of Oracle Corporation and/or its
11 affiliates. Other names may be trademarks of their respective
12 owners.
13 
14 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
15 
16 mysql> 

OK~至此安装成功

Fork me on GitHub