`
dreamoftch
  • 浏览: 495558 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

CentOS安装MySQL

阅读更多

 

转载:Installing MySQL Server on CentOS

 

MySQL 事务:

 

mysql> create table test(id int not null primary key auto_increment, name varchar(16)) engine=InnoDB;
Query OK, 0 rows affected (0.06 sec)

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test(name) values('aaaaaai');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+----+---------+
| id | name    |
+----+---------+
|  1 | aaaaaai |
+----+---------+
1 row in set (0.00 sec)

mysql> savepoint sp1;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test(name) values('sp1');
Query OK, 1 row affected (0.00 sec)

mysql> savepoint sp2;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test(name) values('sp2');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+----+---------+
| id | name    |
+----+---------+
|  1 | aaaaaai |
|  4 | sp1     |
|  5 | sp2     |
+----+---------+
3 rows in set (0.00 sec)

mysql> rollback to sp2;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;
+----+---------+
| id | name    |
+----+---------+
|  1 | aaaaaai |
|  4 | sp1     |
+----+---------+
2 rows in set (0.00 sec)

mysql> rollback to sp1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;
+----+---------+
| id | name    |
+----+---------+
|  1 | aaaaaai |
+----+---------+
1 row in set (0.00 sec)

 

 

 

 

Installing MySQL Server on CentOS

 

MySQL is an open-source relational database. For those unfamiliar with these terms, a database is where an application keeps its data, and relational refers to how the data is organized and accessed within the database. SQL refers to the language used by application queries to retrieve and store data: Structured Query Language.

MySQL is free and widely used, meaning that you can find a large amount of application support, tools, and community help for it. MySQL is a safe choice if you know that you need a database but don't know much about all of the available the options.

This article describes a basic installation of a MySQL database server on CentOS Linux, just enough to get you started. Remember that you might need to install other packages to let applications use MySQL, like extensions for PHP. Check your application documentation for details.

Install MySQL

Install the MySQL server through the CentOS package manager by running the following commands at a command prompt:

 
sudo yum install mysql-server
sudo /sbin/service mysqld start

Then, run the following command:

 
sudo /usr/bin/mysql_secure_installation

Press enter to give no password for root when that program asks for it. To apply some reasonable security to your new MySQL server answer "yes" to all the questions that the program asks. In order, those questions enable you set the root password, remove anonymous users, disable remote root logins, delete the test database that the installer included, and then reload the privileges so that your changes will take effect.

Allow access from other machines

If you have iptables enabled and want to connect to the MySQL database from another machine, you need to open a port in your server's firewall (the default port is 3306). You don't need to do this if the application using MySQL is running on the same machine.

If you do need to open a port, you can use the following rules in iptables to open port 3306:

 
-I INPUT -p tcp --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT
-I OUTPUT -p tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT

NOTE: The iptables command was deliberately left out of the iptables rules in the instructions above. Some people using distributions that do not have their own iptables service might instead have a rules file they can import usingiptables-restore. The format of the lines in that file would be similar to the format used above: iptables options without the iptables command in front of them. For this reason, the instructions in this article represent a compromise. It is easy to paste the lines into a rules file, and they can be used with the iptables command instead.

Launch MySQL

Now that MySQL is installed, you can verify that it's running by trying to launch it:

 
sudo /sbin/service mysqld start

If MySQL is already running, you will receive a message to that effect.

Launch at restart

To ensure that the MySQL server will launch when the machine is restarted, run the following command:

 
sudo chkconfig mysqld on

That makes sure your machine will launch the MySQL server when it reboots.

The mysql shell

There is more than one way to work with a MySQL server, but this article focuses on the most basic and compatible approach: The mysql shell. At the command prompt, run the following command to launch the mysql shell and enter it as the root user:

 
/usr/bin/mysql -u root -p

When you're prompted for a password, enter the one that you set at installation or, if you haven't set one, just press enter to submit no password. The following mysql shell prompt should appear:

 
mysql>

Set the root password

Since you have just installed your MySQL database server, the root account within MySQL has no password set yet. You should change that by running the following commands:

 
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root --password='new-password' -h hostname-of-your-server 'new-password'

NOTE: This article shows SQL commands in all capitals, but you can also type them in lowercase. The commands are shown capitalized by convention, to make them stand out from field names and other data that's being manipulated.

Find database users

As mentioned in the preceding section, MySQL stores the user information in its own database. The name of the database is "mysql". Inside that database, the user information is in a "table", a dataset, named "User". If you want to see what users are set up in MySQL table, or dataset, named "user".

 
SELECT User, Host, Password FROM mysql.user;

Following are descriptions of the parts of that command:

  • The SELECT command tells MySQL that you are asking for data.

  • The User, Host, Password part tells MySQL what fields you want it to look in. Fields are categories for the data in a table. In this case, you are looking for the username, the host associated with the username, and the encrypted password entry.

  • The FROM mysql.user part of the command tells MySQL to get the data from the mysql database and the user table.

  • The command ends with a semicolon.

Ending SQL Queries with a Semicolon

All SQL queries end in a semicolon. MySQL does not process a query until you type a semicolon.

This means that you can break up queries onto multiple lines to make them easier to read. For example, the preceding command also works if you enter it on multiple lines in the mysql shell, as follows:

 
mysql> SELECT User, Host, Password
    -> FROM mysql.user;

When you press enter after the Password part, you get a new line, so you can keep typing. The > symbol indicates that you are still in the middle of a statement. You can type a semicolon by itself to end a command if you forget to type it on the same line as the command.

User hosts

Following is example output for the preceding query:

 
SELECT User, Host, Password FROM mysql.user;
+------------------+-----------+-------------------------------------------+
| User             | Host      | Password                                  |
+------------------+-----------+-------------------------------------------+
| root             | localhost | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
| root             | demohost  | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
| root             | 127.0.0.1 | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
|                  | %         |                                           |
+------------------+-----------+-------------------------------------------+

Users are associated with a host, specifically the host to which they connect. The "root" user in this example is defined for localhost, for the IP address of localhost, and the hostname of the server ("demohost" in this example). You usually need to set a user for only one host, the one from which you typically connect.

If you're running your application on the same machine as the MySQL server the host it connects to by default is "localhost". Any new users that you create must have "localhost" in their "host" field.

If your application connects remotely, the "host" entry that MySQL looks for is the IP address or DNS hostname of the remote machine (the one from which the client is coming).

A special value for the host is %, as you can see in the preceding output for the blank, or anonymous, user (see the following section). The % symbol is a wildcard that applies to any host value. You usually don't want to use that because it's more secure to limit access specifically to trusted hosts.

Anonymous users

In the example output, one entry has a host value but no username or password. That's an "anonymous user". When a client connects with no username specified, it's trying to connect as an anonymous user.

You usually don't want any anonymous users, but some MySQL installations include one by default. If you see one, you should either delete the user (refer to the username with empty quotes, like '') or set a password for it. Both tasks are covered later in this series of articles.

Create a database

There is a difference between database server and an actual database, even though those terms are often used interchangeably. MySQL is a database server, meaning that it keeps track of databases and controls access to them. An actual database is where all the data goes is stored, and it is the database that applications are trying to access when they interact with MySQL.

Some applications create a database as part of their setup process, but others require you to create a database and tell the application about it. Fortunately, creating a database is simple.

To create a database, log in to the mysql shell and run the following command, replacing demodb with the name of the database that you want to create:

 
CREATE DATABASE demodb;

The database is created. You can verify its creation by running a query to list all databases. The following example shows the query and example output:

 
SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| demodb             |
| mysql              |
+--------------------+
3 rows in set (0.00 sec)

Add a database user

When applications connect to the database using the root user, they usually have more privileges than they need. You can create a new user that applications can use to connect to the new database. In the following example, a user named demouser is created.

To create a new user, run the following command in the mysql shell:

 
CREATE USER 'demouser'@'localhost' IDENTIFIED BY 'demopassword';

You can verify that the user was created by running that "SELECT" query again:

 
SELECT User, Host, Password FROM mysql.user;
+------------------+-----------+-------------------------------------------+
| User | Host | Password                                                   |
+------------------+-----------+-------------------------------------------+
| root     | localhost | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19         |
| root     | demohost  | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19         |
| root     | 127.0.0.1 | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19         |
| demouser | localhost | *0756A562377EDF6ED3AC45A00B356AAE6D3C6BB6         |
+------------------+-----------+-------------------------------------------+

Grant database user permissions

Right after you create a new user, it has no privileges. The user can be used to log in to MySQL, but it can't be used to make any database changes. Give the user full permissions for your new database by running the following commmand:

 
GRANT ALL PRIVILEGES ON demodb.* to demouser@localhost;

Then, flush the privileges to make the change take effect.

 
FLUSH PRIVILEGES;

To verify that the privileges were set, run the following command:

 
SHOW GRANTS FOR 'demouser'@'localhost';

MySQL returns the commands needed to reproduce that user's permissions if you were to rebuild the server. The "USAGE on ." part basically means that the user gets no privileges on anything by default. That command is overridden by the second command, which is the grant you ran for the new database.

 
+-----------------------------------------------------------------------------------------------------------------+
| Grants for demouser@localhost                                                                                   |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'demouser'@'localhost' IDENTIFIED BY PASSWORD '*0756A562377EDF6ED3AC45A00B356AAE6D3C6BB6' |
| GRANT ALL PRIVILEGES ON `demodb`.* TO 'demouser'@'localhost'                                                    |
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

Revoking privileges

Sometimes you might need to revoke (remove) privileges form a user, for different reason. For example: you were granting ALL privileges to 'demouser'@'localhost', but by accident (can happen to the best of us any time!) instead of granting them only on the demodb database, you granted them to all other databases too:

 
+-----------------------------------------------------------------------------------------------------------------+
| Grants for demouser@localhost                                                                                   |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'demouser'@'localhost' IDENTIFIED BY PASSWORD '*0756A562377EDF6ED3AC45A00B356AAE6D3C6BB6' |
| GRANT ALL PRIVILEGES ON *.* TO 'demouser'@'localhost'                                                           |
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

After realizing your mistake, you decided to do something to correct it. The easiest way is to use a REVOKE statement, followed by GRANT statement to apply correct privileges.

 
REVOKE ALL ON *.* FROM demouser@localhost;
GRANT ALL PRIVILEGES ON demodb.* to demouser@localhost;
SHOW GRANTS FOR 'demouser'@'localhost';

+-----------------------------------------------------------------------------------------------------------------+
| Grants for demouser@localhost                                                                                   |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'demouser'@'localhost' IDENTIFIED BY PASSWORD '*0756A562377EDF6ED3AC45A00B356AAE6D3C6BB6' |
| GRANT ALL PRIVILEGES ON *.* TO 'demouser'@'localhost'                                                           |
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

Now your user has correct permission, and therefore your database server is slightly more secure (granting privileges likeALL on *.* is deemed as a very bad practice). You should also read official MySQL documentation regarding possible privilege choices, to grant only those privileges truly needed, rather than using ALL.

Summary

If you're just creating a database and a user, you are done. The concepts covered here should give you a solid grounding from which to learn more.

分享到:
评论

相关推荐

    CentOS安装MySQL5.7

    ### CentOS安装MySQL 5.7知识点详解 #### 一、环境准备与系统要求 在开始安装MySQL 5.7之前,我们需要确保系统满足以下条件: - **操作系统**:CentOS 7.x或更高版本。 - **硬件配置**:至少1GB内存,推荐2GB以上;...

    CentOS安装MySQL 5.5

    ### CentOS安装MySQL 5.5知识点详解 #### 1. 概述 本文档旨在指导如何在CentOS系统上从源代码编译、安装并配置MySQL 5.5数据库管理系统。该过程涉及到软件环境的准备、编译工具的安装、MySQL源代码的编译与配置等...

    centos7.0安装mysql

    centos7.0安装mysql centos7.0安装mysql centos7.0安装mysql

    CentOS下mysql成功安装

    CentOS下MySQL成功安装 MySQL是一种流行的关系数据库管理系统,广泛应用于Web应用程序和企业级应用程序中。在CentOS操作系统中,安装MySQL服务器需要经过一系列的步骤,包括下载、编译、安装、初始化数据库、注册...

    centos安装mysql主从步骤

    根据提供的文件信息,这里将详细解释如何在 CentOS 下安装 MySQL 主从配置的步骤。以下是具体的安装过程: ### 1. 确认 CentOS 版本 确保系统版本为 CentOS 6.3,可以通过命令行输入 `cat /etc/centos-release` 来...

    Linux+安装mysql+centos+centos安装mysql

    centos 安装mysql5.7 Linux下centos安装mysql5.7 超详细步骤,带你手把手安装mysql

    CentOS 7 安装 MySQL

    CentOS 7 安装 MySQL

    Mysql 教程 - centos安装 Mysql 教程.pdf

    Mysql 教程 - CentOS 安装 Mysql 教程 本教程主要介绍了在 CentOS 平台上安装和卸载 MySQL 的步骤,涵盖了使用 yum 命令安装 MySQL 的过程,并详细解释了编辑 yum 源文件、选择 MySQL 版本、检查安装结果等步骤。 ...

    centos安装mysql5.6.docx

    CentOS 6 安装 MySQL 5.6 MySQL 是一个流行的开源关系数据库管理系统,广泛应用于 Web 应用程序中。 CentOS 6 是一个基于 Linux 的操作系统,本文将指导您如何在 CentOS 6 上安装 MySQL 5.6。 关闭 SELinux ...

    Centos安装mysql数据库必须包libaio-0.3.107-10.el6.x86_64

    用于Centos操作系统,安装mysql数据时,需要的libaio文件。如果您在安装时候,报这个错误可以下载,如下: mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such ...

    CentOS安装Mysql_Cluster集群

    #### 三、检查现有MySQL安装 - **检查命令**: 使用`rpm -qa | grep mysql`检查已安装的MySQL相关信息。 - **卸载旧版本**: 如果检测到已有安装,则需使用`yum remove mysql-server mysql-client mysql-libs`卸载。 ...

    centos 安装mysql8 亲自测试成功

    centos 安装mysql8 亲自测试成功

    centos安装mysql5.5.txt

    ### CentOS 安装 MySQL 5.5 的详细步骤与注意事项 #### 一、背景介绍 在 CentOS 系统上安装 MySQL 数据库是一项常见的任务,尤其是对于 Web 开发者和系统管理员来说。本文将详细介绍如何在 CentOS 系统上安装 ...

    centos7安装mysql5.7

    在本地虚拟机的场合,需要更换yum安装网络地址配置文件CentOS-Base.repo 从阿里云服务器下载一份CentOS-Base.repo文件存到本地

    centos安装mysql5.7.docx

    CentOS 7.6 安装 MySQL 5.7 MySQL 是一个开源的关系数据库管理系统,广泛应用于 web 开发中。下面是 CentOS 7.6 安装 MySQL 5.7 的步骤。 安装 MySQL 首先,需要新建文件夹 `/opt/mysql` 并 cd 进去。然后,下载...

    虚拟机CentOS安装mysql数据库.docx

    ### 虚拟机 CentOS 安装 MySQL 数据库及配置读写分离 #### 一、环境准备 在本文档中,我们将详细介绍如何在虚拟机 CentOS 上安装 MySQL 数据库,并进行必要的网络配置以支持读写分离的功能。为了实现这一目标,...

    2-CentOS安装MySQL1

    2-CentOS安装MySQL1

Global site tag (gtag.js) - Google Analytics