欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在Linux中在线安装MySQL数据库

程序员文章站 2022-05-27 13:40:35
...

1 首先保证我们linux 系提可以上网

ping www.baidu.com      
如果可以ping通,那么说名我们主机可以上网。

2 yum install 在线安装mysql数据库

#yum intall  -y   mysql   mysql-server 
	
		-y 的参数意思是安装过程中不需要人为确认。

3 安装完成以后 你会看到这样的信息

......

		Installed:
		  mysql.i686 0:5.1.73-8.el6_8   
		  mysql-server.i686 0:5.1.73-8.el6_8                                                               

		Complete!
	有这个提示,基本就安装成功了。

4 访问mysql数据

1 检查mysql数据库服务的状态 
		#service  mysqld  status
2 刚安装完mysql,数据库默认是关闭的。需要手工启动数据库
	#service  mysqld start 
3 连接数据库 ,首次安装mysql数据库,是没有密码的,默认的管理员账号 是 root 
		#mysql  -uroot         回车即可
 检查是否可以正常登录。
		[[email protected] fl02]# mysql -uroot
		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>
4 可以顺便查一下 mysql数据库中的数据库有哪些? 有哪些表
		mysql>show databses ;
			mysql> show databases;
			+--------------------+
			| Database           |
			+--------------------+
			| information_schema |
			| mysql              |
			| test               |
			+--------------------+
			3 rows in set (0.00 sec)

		
		mysql>use mysql;
				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 ;
			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)

			mysql> 

-------------------------------------------- 我是分割线 -----------------------------------------------------
1 首次安装mysql数据库,存在密码的问题

方法一 ,卸载mysql 重新安装【重装 mysql 不一定能解决问题】

1  停止mysql数据库
			#service mysqld stop
2  卸载mysql的2个主要包 ,mysql-server  mysql
			#yum remove -y  mysql  mysql-server 
3  检查是否卸载完成
			#rpm -qa | grep mysql 
		
		完成上面的步骤以后 ,重新安装mysql 

方法二,下面的操作首先必须有管理员权限。

1 先检查mysql服务 ,主要看下mysql服务能否正常启动。
			服务必须启动,说明数据库正常,如果连服务都启动不了 ,那就洗洗睡吧。
2 修改mysql的配置文件  /etc/my.cnf 
			在配置信息中间任意位置,插入一行代码
				skip-grant-tables
			保存并退出
3 重启mysql数据库
			#service  mysqld restart
	重启成功后,重新访问mysql数据库。这时候就不需要密码验证了 。

----------------------------------------------------- 我是分割线 ----------------------------------------------

知道旧密码的情况下,修改mysql的管理员密码。

#mysqladmin  -uroot [-p旧密码]  password 新密码 

	首次访问mysql,没有初始密码的时候,[-p旧密码]   可以不写。

不知道旧密码的情况下,修改新密码

1 能访问数据库,如果不能,那么修改mysql配置文件,让密码验证失效。
		#vi /etc/my.cnf
		
		在里面加入一行代码:  skip-grant-tables
		保存并退出
		重启mysql数据库
		#service mysqld restart
		
		重启成功后,再访问数据库就不需要密码了。
2 数据库中修改用户的密码。
	
	[[email protected] ~]# mysql -uroot
	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> 
	mysql> show databases;
	+--------------------+
	| Database           |
	+--------------------+
	| information_schema |
	| mysql              |
	| test               |
	+--------------------+
	3 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)

	mysql> select user,host,passwd from user;
	ERROR 1054 (42S22): Unknown column 'passwd' in 'field list'
	mysql> select user,host,password from user;
	+------+-----------+-------------------------------------------+
	| user | host      | password                                  |
	+------+-----------+-------------------------------------------+
	| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
	| root | linux02   |                                           |
	| root | 127.0.0.1 |                                           |
	|      | localhost |                                           |
	|      | linux02   |                                           |
	+------+-----------+-------------------------------------------+
	5 rows in set (0.00 sec)

	mysql> update user set password = password('654321')  where user = 'root' and host ='localhost';
	Query OK, 1 row affected (0.01 sec)
	Rows matched: 1  Changed: 1  Warnings: 0

	mysql> flush privileges;
	Query OK, 0 rows affected (0.00 sec)

	mysql> exit;
	Bye
	[[email protected] ~]# vi /etc/my.cnf 
	
		光标移动到 skip-grant-table 这一行,然后输入 dd 
		保存并退出。
		
	
	[[email protected] ~]# service mysqld restart
	停止 mysqld:                                              [确定]
	正在启动 mysqld:                                          [确定]
	
		重启mysql数据库之后,密码验证会重新生效。
[[email protected] ~]# mysql -uroot -p654321
	Welcome to the MySQL monitor.  Commands end with ; or \g.
	Your MySQL connection id is 4
	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> 

远程连接数据库

1 数据库必须启动

[[email protected] ~]# chkconfig mysqld on  -- 将mysql数据库的服务,设置为开机启动
	
	[[email protected] ~]# chkconfig --list|grep mysql -- 检查开机启动是否设置成功
	mysqld      0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
**2  必须在数据库中授权,哪些IP可以访问mysql数据库, 可以直接使用通配符 %**
#mysql -uroot -p654321
		
		mysql>use mysql;         -- 选择mysql自身的数据库。
		
		mysql> select user,host from user;
		+------+-----------+
		| user | host      |    -- host是用户可以访问数据库的主机地址/ip/名称
		+------+-----------+
		| root | 127.0.0.1 |
		|      | linux02   |
		| root | linux02   |
		|      | localhost |
		| root | localhost |
		+------+-----------+

		mysql>update  user  set host = '%' where host ='localhost';
		
		mysql> select host,user from user;
		+-----------+------+
		| host      | user |
		+-----------+------+
		| %         |      |
		| %         | root |
		| 127.0.0.1 | root |
		| linux02   |      |
		| linux02   | root |
		+------+-----------+

3 防火墙不能阻止外面的主机访问mysql

1 在防火墙中增加端口
			#vi /etc/sysconfig/iptables
			
	-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
2 关闭防火墙
			#service iptables stop 

4 通过windows上的数据库客户端,尝试连接数据库。

1  数据库服务器的地址  ip 比如 192.168.100.36
2  端口号	mysql的默认 3306 
3  用户名/密码   root/123456
相关标签: Linux