首页 » MySQL » Linux系统安装mysql服务

Linux系统安装mysql服务

原文 http://blog.csdn.net/fantasic_van/article/details/79187675

2018-01-29 02:01:11阅读(294)

1、使用yum命令安装
yum -y install mysql-server
2、安装成功后,设置开机启动
chkconfig mysqld on
3、启动mysql服务
service mysqld start
4、 设置root用户密码
mysql> select user,host,password from mysql.user;
+------+-----------------------+----------+
| user | host                  | password |
+------+-----------------------+----------+
| root | localhost             |          |
| root | localhost.localdomain |          |
| root | 127.0.0.1             |          |
|      | localhost             |          |
|      | localhost.localdomain |          |
+------+-----------------------+----------+
5 rows in set (0.00 sec)
可以看到,默认用户root是没有密码的,需要手动设置
mysql> set password for root@localhost=password('root');
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,password from mysql.user;
+------+-----------------------+-------------------------------------------+
| user | host                  | password                                  |
+------+-----------------------+-------------------------------------------+
| root | localhost             | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | localhost.localdomain |                                           |
| root | 127.0.0.1             |                                           |
|      | localhost             |                                           |
|      | localhost.localdomain |                                           |
+------+-----------------------+-------------------------------------------+
5 rows in set (0.00 sec)
这里就修改完成了,不过密码已经被加密了
5、使用新密码登录
mysql> exit
Bye
[root@localhost yum.repos.d]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
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> 
使用新密码登录完成
6、创建mysql的新用户,test
mysql> create user 'test'@'%' identified by 'test';  
Query OK, 0 rows affected (0.00 sec)  
创建成功,查看一下结果
mysql> select user,host,password from mysql.user;
+------+-----------------------+-------------------------------------------+
| user | host                  | password                                  |
+------+-----------------------+-------------------------------------------+
| root | localhost             | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | localhost.localdomain |                                           |
| root | 127.0.0.1             |                                           |
|      | localhost             |                                           |
|      | localhost.localdomain |                                           |
| test | %                     | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 |
+------+-----------------------+-------------------------------------------+
6 rows in set (0.00 sec)
发现已经多了test用户
7、给test用户授权,可以使得test用户从外部登录和本地登录
mysql> grant all privileges on *.* to 'test'@'localhost' identified by 'test';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'test'@'%' identified by 'test';
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,password from mysql.user;
+------+-----------------------+-------------------------------------------+
| user | host                  | password                                  |
+------+-----------------------+-------------------------------------------+
| root | localhost             | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | localhost.localdomain |                                           |
| root | 127.0.0.1             |                                           |
|      | localhost             |                                           |
|      | localhost.localdomain |                                           |
| test | %                     | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 |
| test | localhost             | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 |
+------+-----------------------+-------------------------------------------+
7 rows in set (0.00 sec)
ps: @符号的左边是用户名,右边是ip地址或者域名或者%,其中%表示任何地址都能访问
8、查看mysql的默认引擎
mysql> show engines;
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine     | Support | Comment                                                    | Transactions | XA   | Savepoints |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                      | NO           | NO   | NO         |
| CSV        | YES     | CSV storage engine                                         | NO           | NO   | NO         |
| MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance     | NO           | NO   | NO         |
| InnoDB     | YES     | Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables  | NO           | NO   | NO         |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
5 rows in set (0.00 sec)
从上看的出,现在默认的引擎是MyISAM,是不支持事务的,只有InnoDB支持失误,因此需要把默认引擎改成InnoDB
9、修改mysql的默认引擎为InnoDB 1) 退出mysql,并停止服务
mysql> exit;
Bye
[root@localhost yum.repos.d]# service mysqld stop;
Stopping mysqld:                                           [  OK  ]
2) 修改myql的配置文件,/etc/my.cnf
① vim /etc/my.cnf
② 在 [mysqld]下,加入: 
default-storage-engine=InnoDB
③ 保存退出
④ 重启服务
[root@localhost etc]# service mysqld start
Starting mysqld:                                           [  OK  ]
10、重新查看mysql默认引擎
[root@localhost etc]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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> show engines;
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine     | Support | Comment                                                    | Transactions | XA   | Savepoints |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                      | NO           | NO   | NO         |
| CSV        | YES     | CSV storage engine                                         | NO           | NO   | NO         |
| MyISAM     | YES     | Default engine as of MySQL 3.23 with great performance     | NO           | NO   | NO         |
| InnoDB     | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables  | NO           | NO   | NO         |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
5 rows in set (0.00 sec)
可以看到,默认引擎已经修改为InnoDB了
11、为了能使mysql能在外部系统访问,需要Linux防火墙开放3306端口
① vim /etc/sysconfig/iptables
② 修改后的防火墙配置
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
③ 重启防火墙
[root@localhost etc]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]
12、 步骤总结

① 使用yum在线安装mysql-server
② 设置root用户密码
③ 设置自定义用户,并赋权
④ 修改myql默认引擎为InnoDB,以支持事务
⑤ 修改linux防火墙配置,开放mysql的默认端口3306,以便外部系统能够访问

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹