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.
The next article covers some basic security and stability checks by looking at the MySQL server's configuration files and a few key tools.
连接:
http://www.rackspace.com/knowledge_center/article/installing-mysql-server-on-centos
Installing MySQL Server on CentOS的更多相关文章
- Installing MySQL Server
Installing MySQL Server Here we will learn how to Compile and Install the MySQL Server from source c ...
- mysql Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’
mysql Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ 今天在linux中安装了mys ...
- Installing MySQL 5.7.23 on CentOS 7
Installing MySQL 5.7.23 on CentOS 7 1. 安装前检查 1.1 检查NUMA是否开启 NUMA为什么要咋MySQL中禁用? MySQL是单进程多线程架构数据库,当nu ...
- CentOS 7中如何安装mysql server
以前一直用RHEL 6.3和6.4,系统盘里自带了mysql server,配置好yum源后,直接yum install mysql-server就可安装mysql服务器端了,最近用CentOS 7. ...
- CentOS的MySQL报错:Can't connect to MySQL server
原文链接: http://www.centoscn.com/CentosBug/softbug/2015/0622/5709.html 问题描述: 使用客户端远程登录连接基于CentOS 6.5服务器 ...
- Centos 7 官网下载安装mysql server 5.6
Centos 7 官网下载安装 mysql server # wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rp ...
- CentOS下httpd下php 连接mysql 本机可以,外网报错Could not connect: Can't connect to MySQL server on '127.0.0.1' (13)2003 原因解析
php代码很简单: $server="127.0.0.1"; println("Begin"); $link = mysql_connect($server,& ...
- CentOS 7 -防火墙设置--安装数据库,远程连接报错--Can't connect to MySQL server on localhost (10061)
前提简介:在CentOS 7 上安装了mysql5.7版本,已设置了远程访问权限,但是其他服务器无法访问到此Mysql,提示[Can't connect to MySQL server on loca ...
- 在centos中php 在连接mysql的时候,出现Can't connect to MySQL server on 'XXX' (13)
原文连接:http://hi.baidu.com/zwfec/item/64ef5ed9bf1cb3feca0c397c 红色的是命令 SQLSTATE[HY000] [2003] Can't con ...
随机推荐
- idea让字体更圆滑
个人用myeclipse习惯了, 转战idea 觉得自己看题来很方很死板的样子. 然后更换了其字体,如果喜欢你可以试试. 点击file --> setting.. --> 把编辑的字体改成 ...
- ASP.NET 页生命周期概述
ASP.NET 页生命周期概述 Visual Studio 2005 ASP.NET 页运行时,此页将经历一个生命周期,在生命周期中将执行一系列处理步骤.这些步骤包括初始化.实例化控件.还原和维 ...
- [转载]能不能同时用static和const修饰类的成员函数?
题目(一):我们可以用static修饰一个类的成员函数,也可以用const修饰类的成员函数(写在函数的最后表示不能修改成员变量,不是指写在前面表示返回值为常量).请问:能不能同时用static和con ...
- Light OJ 1068
数位DP #include <cstdio> #include <cstring> using namespace std; ; ; long long n; int f[MA ...
- FFMpeg ver 20160213-git-588e2e3 滤镜中英文对照
1 FFMpeg ver 20160213-git-588e2e3 滤镜中英文对照 2016.02.18 by 1CM 2 T.. = Timeline support 3 支持时间轴 4 .S. = ...
- Greedy:Sum of Consecutive Prime Numbers(POJ 2739)
素数之和 题目大意:一些整数可以表示成一个连续素数之和,给定一个整数要你找出可以表示这一个整数的连续整数序列的个数 方法:打表,然后用游标卡尺法即可 #include <iostream> ...
- 将项目导入eclipse中出现的jsp页面报错解决
- 【leetcode】Reverse Linked List(easy)
Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...
- 【python】入门学习(三)
for循环 for i in range(): #注意冒号 range中默认从0开始 或者从指定的数字开始 到给定数字的前一个数字结束 递增递减皆是如此 for循环提供变量的自动初始化 for i ...
- 【编程题目】一串首尾相连的珠子(m 个),有 N 种颜色(N<=10),取出其中一段,要求包含所有 N 中颜色,并使长度最短。
40.百度研发笔试题 2)一串首尾相连的珠子(m 个),有 N 种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有 N 中颜色,并使长度最短.并分析时间复杂度与空间复杂度. 思路: ...