Linux进阶15:Linux下MySQL数据库安装及配置

本文以CentOS6.9为例,介绍MySQL数据库的基本安装和配置,最后达到可以远程访问数据库及建库建表的目的。

安装及启动

安装命令如下。

1
$ sudo yum install mysql mysql-server

启动命令如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ sudo service mysqld start
Initializing MySQL database: 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 demo 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 ]

自启动配置如下。

1
2
3
$ sudo chkconfig mysqld on
$ sudo chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off

配置root密码

有多种方式配置root用户的密码,从上面的第一次启动MySQL服务的信息看,推荐使用mysql_secure_installation配置密码、禁用匿名用户以及删除test数据库。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
$ 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
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.

Disallow root login remotely? [Y/n] y

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

以上操作主要包含以下内容。
(1) Enter current password for root (enter for none):,输入root用户密码,第一次设置直接输入回车。
(2) Set root password? [Y/n],设置root用户密码。
(3) Remove anonymous users? [Y/n],删除匿名用户。
(4) Disallow root login remotely? [Y/n],禁用root远程登录。
(5) Remove test database and access to it? [Y/n], 删除test数据库和访问权限。
(6) Reload privilege tables now? [Y/n],重新加载表权限。

登录及退出MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.73 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> quit
Bye

新建用户及配置权限

新建learnit用户,可以从任意ip登录('learnit'@'%'),访问任意库表(*.*),密码123456,开通全部权限(ALL PRIVILEGES)。

1
2
mysql> GRANT ALL PRIVILEGES ON *.* TO 'learnit'@'%'
-> IDENTIFIED BY '123456' WITH GRANT OPTION;

测试远程连接MySQL。其中-h参数在非本机访问时使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ mysql -u learnit -h 192.168.80.6 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2018, 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>

字符集设置

MySQL默认字符集是latin1,中文会出乱码,需要修改为utf8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

修改/etc/my.cnf。内容如下。

1
2
3
4
5
[mysqld]
character-set-server=utf8

[client]
default-character-set=utf8

修改完成后重启服务。

1
$ sudo service mysqld restart

再次查看字符集,已成功改为utf8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

CentOS7的MySQL数据库安装

Oracle收购了MySQL后,CentOS组织担心Oracle会对MySQL收费,故CentOS7放弃了MySQL,改用了MariaDB,无法直接通过Yum安装。通过增加MySQL Yum源的方式解决。

获取MySQL yum源

1
2
$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
$ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

安装这个包后,会获得两个MySQL的yum repo源。

1
2
3
$ ls -1 /etc/yum.repos.d/mysql-community*
/etc/yum.repos.d/mysql-community.repo
/etc/yum.repos.d/mysql-community-source.repo

安装MySQL、启动服务和配置自启动

1
2
3
$ sudo yum install mysql-server
$ sudo systemctl start mysqld
$ sudo systemctl enable mysqld

如果报错,那么与mariadb-server有冲突,将其删除。

1
$ sudo rpm -e mariadb-server akonadi-mysql

其他

(1) 脚本注释

1
2
3
# 注释内容
--  注释内容
/* 注释内容 */

(2) 竖排显示

1
SELECT * FROM table\G

(3) 显示当前使用的数据库

1
SELECT database();

如果本文对你有所帮助,请小额赞助
~~ EOF ~~