CentOS下yum安装配置MySQL

来源:互联网 发布:mac微信手机 编辑:程序博客网 时间:2024/05/16 09:57
1. 安装mysql

打开终端,输入:

yum install mysql mysql-server

安装成功后会提示Completed!


2.为MySQL帐户进行安全设置:

 mysql_secure_installation

  这时会出错,ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2);

原因是权限不够,服务器未启动,应先启动MySQL服务

[maybezi@localhost ~]$ service mysqld start

touch: 无法创建"/var/log/mysqld.log": 权限不够
chown: 正在更改"/var/log/mysqld.log" 的所有者: 不允许的操作
chmod: 更改"/var/log/mysqld.log" 的权限: 不允许的操作
chown: 正在更改"/var/lib/mysql" 的所有者: 不允许的操作
chmod: 更改"/var/lib/mysql" 的权限: 不允许的操作
初始化 MySQL 数据库: mkdir: 无法创建目录"/var/lib/mysql/mysql": 权限不够
chmod: 无法访问"/var/lib/mysql/mysql": 没有那个文件或目录
mkdir: 无法创建目录"/var/lib/mysql/test": 权限不够
chmod: 无法访问"/var/lib/mysql/test": 没有那个文件或目录
Installing MySQL system tables...
121215 13:18:16 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
121215 13:18:16 [Warning] Can't create test file /var/lib/mysql/localhost.lower-test
ERROR: 1049  Unknown database 'mysql'
121215 13:18:16 [ERROR] Aborting
121215 13:18:16 [Note] /usr/libexec/mysqld: Shutdown complete
Installation of system tables failed!  Examine the logs in
/var/lib/mysql for more information.
You can try to start the mysqld daemon with:
    shell> /usr/libexec/mysqld --skip-grant &
and use the command line tool /usr/bin/mysql
to connect to the mysql database and look at the grant tables:
    shell> /usr/bin/mysql -u root mysql
    mysql> show tables
Try 'mysqld --help' if you have problems with paths.  Using --log
gives you a log in /var/lib/mysql that may be helpful.
Please consult the MySQL manual section
'Problems running mysql_install_db', and the manual section that
describes problems on your OS.  Another information source are the
MySQL email archives available at http://lists.mysql.com/.
Please check all of the above before mailing us!  And remember, if
you do mail us, you MUST use the /usr/bin/mysqlbug script!
                                                           [失败]

chown: 正在更改"/var/lib/mysql" 的所有者: 不允许的操作

所以切换到ROOT用户启动mysqld

[maybezi@localhost ~]$ su
密码:
[root@localhost maybezi]# service mysqld start
初始化 MySQL 数据库: 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 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!
                                                           [确定]

正在启动 mysqld:                                          [确定]

3.安全及密码设置:

[root@localhost maybezi]# 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.
Set root password? [Y/n] y            <----- 设置ROOT密码,回车
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]    <----- 删除匿名用户
 ... 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]    <----- 禁止远程登录
 ... 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.
Remove test database and access to it? [Y/n] 
 - 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] 
 ... 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!


至此MySQL就已经基本配置好了

接下来可以登录MySQl测试一下

[root@localhost maybezi]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.1.66 Source distribution
Copyright (c) 2000, 2012, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 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)


OK,可以进行其他操作了

原创粉丝点击