Linux服务器安装MYSQL数据库

Linux服务器安装MYSQL数据库

1.检测服务器是否自带MySQL

# rpm -qa | grep mysql
mysql-libs-5.1.73-7.el6.x86_64

有,说明已经自带了

# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

看安装目录在哪里

# find / -name mysql
/usr/share/mysql
/usr/lib64/mysql

如何使用自带的数据库,未操作

​ 卸载自带的安装包mysql-libs-5.1.73-7.el6.x86_64

# rpm -e mysql-libs-5.1.73-7.el6.x86_64  ---->有依赖
error: Failed dependencies:
    libmysqlclient.so.16()(64bit) is needed by (installed) postfix-2:2.6.6-6.el6_7.1.x86_64
    libmysqlclient.so.16(libmysqlclient_16)(64bit) is needed by (installed) postfix-2:2.6.6-6.el6_7.1.x86_64
    mysql-libs is needed by (installed) postfix-2:2.6.6-6.el6_7.1.x86_64
[root@cloud etc]# rpm -e mysql-libs-5.1.73-7.el6.x86_64 --nodeps   --->强制删除

2.安装MySQL

搜索MySQL

# yum list | grep mysql
apr-util-mysql.x86_64                      1.3.9-3.el6_0.1               base   
bacula-director-mysql.x86_64               5.0.0-13.el6                  base   
bacula-storage-mysql.x86_64                5.0.0-13.el6                  base   
dovecot-mysql.x86_64                       1:2.0.9-22.el6                base   
freeradius-mysql.x86_64                    2.2.6-7.el6_9                 updates
libdbi-dbd-mysql.x86_64                    0.8.3-5.1.el6                 base   
mod_auth_mysql.x86_64                      1:3.0.0-11.el6_0.1            base   
mysql.x86_64                               5.1.73-8.el6_8                base   
mysql-bench.x86_64                         5.1.73-8.el6_8                base   
mysql-connector-java.noarch                1:5.1.17-6.el6                base   
mysql-connector-odbc.x86_64                5.1.5r1144-7.el6              base   
mysql-devel.i686                           5.1.73-8.el6_8                base   
mysql-devel.x86_64                         5.1.73-8.el6_8                base   
mysql-embedded.i686                        5.1.73-8.el6_8                base   
mysql-embedded.x86_64                      5.1.73-8.el6_8                base   
mysql-embedded-devel.i686                  5.1.73-8.el6_8                base   
mysql-embedded-devel.x86_64                5.1.73-8.el6_8                base   
mysql-libs.i686                            5.1.73-8.el6_8                base   
mysql-libs.x86_64                          5.1.73-8.el6_8                base   
mysql-server.x86_64                        5.1.73-8.el6_8                base   
mysql-test.x86_64                          5.1.73-8.el6_8                base   
pcp-pmda-mysql.x86_64                      3.10.9-9.el6                  base   
php-mysql.x86_64                           5.3.3-49.el6                  base   
qt-mysql.i686                              1:4.6.2-28.el6_5              base   
qt-mysql.x86_64                            1:4.6.2-28.el6_5              base   
rsyslog-mysql.x86_64                       5.8.10-10.el6_6               base   
rsyslog7-mysql.x86_64                      7.4.10-7.el6                  base  

​ 通过输入 yum install -y mysql-server mysql mysql-devel 命令将mysql mysql-server mysql-devel都安装好(注意:安装mysql时我们并不是安装了mysql客户端就相当于安装好了mysql数据库了,我们还需要安装mysql-server服务端才行)

# yum install -y mysql-server mysql mysql-devel
....
Complete!

等待了一番时间后,yum会帮我们选择好安装mysql数据库所需要的软件以及其它附属的一些软件

​ 查看mysql-server是否安装成功

# rpm -qi mysql-server
Name        : mysql-server                 Relocations: (not relocatable)
Version     : 5.1.73                            Vendor: CentOS
Release     : 8.el6_8                       Build Date: Fri 27 Jan 2017 06:25:43 AM HKT
Install Date: Fri 08 Sep 2017 08:06:15 AM HKT      Build Host: c1bm.rdu2.centos.org
Group       : Applications/Databases        Source RPM: mysql-5.1.73-8.el6_8.src.rpm
Size        : 25884131                         License: GPLv2 with exceptions
Signature   : RSA/SHA1, Fri 27 Jan 2017 06:35:28 AM HKT, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : http://www.mysql.com
Summary     : The MySQL server and related files
Description :
MySQL is a multi-user, multi-threaded SQL database server. MySQL is a
client/server implementation consisting of a server daemon (mysqld)
and many different client programs and libraries. This package contains
the MySQL server and some accompanying files and directories.

​ 安装后会多出一个mysqld的服务

3.启动MYSQL

首次启动提示的信息比较多,有一些初始化信息,下次重启就比较少了

# service mysqld start
Initializing MySQL database:  WARNING: The host 'cloud' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
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 cloud 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  ]

# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

3.1启动关闭MySQL

service mysqld start
service mysqld stop
service mysqld restart

4.开机启动MySQL

检查服务是否开机启动,将其设置成开机启动

mysqld             0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭

# chkconfig --list | grep mysqld
mysqld          0:off   1:off   2:off   3:off   4:off   5:off   6:off
[root@cloud etc]# chkconfig mysqld on
[root@cloud etc]# chkconfig --list | grep mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

5.初始化配置MySQL账号信息

首次启动的提示信息:

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 cloud password 'new-password'

给我们的root账号设置密码(注意:这个root账号是mysql的root账号,非Linux的root账号)

# /usr/bin/mysqladmin -u root password '密码'

登录MySQL

# mysql -u root -p
Enter password:  密码输入 
Welcome to the MySQL
....
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

6.mysql数据库的主要配置文件

/etc/my.cnf 这是mysql的主配置文件
/var/lib/mysql   mysql数据库的数据库文件存放位置
/var/log mysql数据库的日志输出存放位置

因为我们的mysql数据库是可以通过网络访问的,并不是一个单机版数据库,其中使用的协议是 tcp/ip 协议,我们都知道mysql数据库绑定的端口号是 3306 ,所以我们可以通过 netstat -anp 命令来查看一下,Linux系统是否在监听 3306 这个端口号:

[root@cloud etc]# netstat -anp 
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      62925/mysqld        


已经监听,结束操作

7.远程连接MySQL数据库

Sequel pro工具连接失败

Connection failed!

Unable to connect to host IP, or the request timed out.

Be sure that the address is correct and that you have the necessary privileges, or try increasing the connection timeout (currently 10 seconds).

MySQL said: Can't connect to MySQL server on 'IP' (61)

​ —>host错误问题

防护墙设置:

# vi /etc/sysconfig/iptables

加入3306端口

-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 8080 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

重启防火墙

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

测试发现还是不行:

开启MySQL远程访问权限 允许远程连接
1、登录服务器,然后运行命令:mysql -u root –p   ,然后输入密码,该步骤是进入数据库。

2、mysql>use mysql;

3、授权:

例如想root使用123456从任何主机连接到mysql服务器: 
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;

如果想允许用户abc从ip为xx的主机连接到mysql服务器,并使用xxx作为密码:
mysql>GRANT ALL PRIVILEGES ON *.* TO 'abc'@'IP' IDENTIFIED BY '密码' WITH GRANT OPTION;

4、刷新权限: mysql>FLUSH PRIVILEGES;

测试成功!

8.其它

8.1 创建一个新的登录用户

# 其中’%’表示客户端可以为任何ip,当然也可以明确规定客户端的ip地址
# root用户登录
mysql -u root -p
>use mysql
mysql> create database databasename;
mysql> create user 'username'@'%' identified by 'userpassword';
mysql> grant create,drop,select,insert,update,delete on databasename.* to 'username'@'%';
mysql>FLUSH PRIVILEGES;

​ 发现没有数据库game权限,原因是先创建用户再授权,最后再创建数据库.因此授权数据库的时候,数据库必须由有权限的账号已经创建好了才行.—>创建表,移除表,增删改查

8.2 有时候创建了用户确说权限不足

—> todo待深入研究

# 删除存在的空用户
> delete from  mysql.user where user = '';

重启登录
# service mysqld restart;

9.Ubuntu上

9.1 配置文件的真正位置

/etc/mysql/mysql.conf.d/mysqld.cnf

10.完全卸载MySQL

10.1 简述
上述方法安装了MySQL之后,版本是5.1版本很低,很多功能使用起来很不方便,而且时间戳的支持也不好,于是打算卸载重新安装更高版本的MySQL.

10.2 卸载方法

]# yum remove  mysql mysql-server mysql-libs mysql-server
....

找到残余,删除所有MySQL的文件
[root@VM_0_7_centos ~]# find / -name mysql
/usr/share/mysql
/var/lib/mysql
/var/lib/mysql/mysql

]# rm -rf /usr/share/mysql
]# rm -rf /var/lib/mysql

11.安装MySQL5.7.20

第一步:下载mysql最新版
# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz --no-check-certificate

第二步:在/usr/local/中解压压缩包,并改名为mysql
software]# cd /usr/local/
l]# tar -xzvf /data/software/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
local]# mv mysql-5.7.20-linux-glibc2.12-x86_64/ mysql

第三步:创建用户组mysql,创建用户mysql并将其添加到用户组mysql中,并赋予读写权限
groupadd mysql

useradd -r -g mysql mysql

chown -R mysql mysql/

chgrp -R mysql mysql/


第四步:创建配置文件
vim /etc/my.cnf

#复制以下内容

[client]
port = 3306
socket = /tmp/mysql.sock

[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#不区分大小写
lower_case_table_names = 1

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

max_connections=5000

default-time_zone = '+8:00'

按ESC保存并关闭,输入如下命令 :wq!

第五步:初始化数据库
#先安装一下这个东东,要不然初始化有可能会报错
log]# yum install libaio
#手动编辑一下日志文件,什么也不用写,直接保存退出
log]# cd /var/log/

log]# vim mysqld.log
:wq

log]# chmod 777 mysqld.log
log]# chown mysql:mysql mysqld.log

整行执行,等待执行完毕
log]#  /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US


第六步:查看初始密码
og]# cat /var/log/mysqld.log

...
0Z 1 [Note] A temporary password is generated for root@localhost: yUe0OqX*eF_V

执行后关注最后一点:root@localhost: 这里就是初始密码


第七步:启动服务,进入mysql,修改初始密码,运行远程连接(这里执行完后,密码将变成:你设置的新密码)

cd /var/run/

mkdir mysqld

chmod 777 mysqld

cd mysqld
vim mysqld.pid

mysqld]# chmod 777 mysqld.pid
]# chown mysql:mysql mysqld.pid

启动MySQL
mysqld]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL SUCCESS!

登录操作
mysqld]# /usr/local/mysql/bin/mysql -u root -p
Enter password: --输入 yUe0OqX*eF_V

修改密码
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

#如果提示必须要修改密码才可以进行操作的话则执行下面操作
set password=password('新密码');
mysql> flush privileges;
mysql> UPDATE `mysql`.`user` SET `Host` = '%',  `User` = 'root'  WHERE (`Host` = 'localhost') AND (`User` = 'root');
Query OK, 1 row affected (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>  UPDATE `mysql`.`user` SET `Host`='%', `User`='root', `Select_priv`='Y', `Insert_priv`='Y', `Update_priv`='Y', `Delete_priv`='Y', `Create_priv`='Y', `Drop_priv`='Y', `Reload_priv`='Y', `Shutdown_priv`='Y', `Process_priv`='Y', `File_priv`='Y', `Grant_priv`='Y', `References_priv`='Y', `Index_priv`='Y', `Alter_priv`='Y', `Show_db_priv`='Y', `Super_priv`='Y', `Create_tmp_table_priv`='Y', `Lock_tables_priv`='Y', `Execute_priv`='Y', `Repl_slave_priv`='Y', `Repl_client_priv`='Y', `Create_view_priv`='Y', `Show_view_priv`='Y', `Create_routine_priv`='Y', `Alter_routine_priv`='Y', `Create_user_priv`='Y', `Event_priv`='Y', `Trigger_priv`='Y', `Create_tablespace_priv`='Y', `ssl_type`='', `ssl_cipher`='', `x509_issuer`='', `x509_subject`='', `max_questions`='0', `max_updates`='0', `max_connections`='0', `max_user_connections`='0', `plugin`='mysql_native_password', `authentication_string`='*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9', `password_expired`='N', `password_last_changed`='2017-11-20 12:41:07', `password_lifetime`=NULL, `account_locked`='N' WHERE  (`User`='root');
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

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


此时不要退出,如果退出,可能会报错
]# /usr/local/mysql/bin/mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

远程用户建立(可以自动创建新用户并且拥有最高权限,或者修改原账户权限)
grant all privileges on *.* to '新用户名'@'%' identified by '新密码';
flush privileges;


第八步:开机自启
cd /usr/local/mysql/support-files

cp mysql.server /etc/init.d/mysqld

chkconfig --add mysqld

第九步:使用service mysqld命令启动/停止服务
su - mysql

service mysqld start/stop/restart

vim /etc/profile

添加系统路径
export PATH=/usr/local/mysql/bin:$PATH

source /etc/profile


mysql的启动与停止

# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@VM_0_7_centos ~]# service mysqld start
Starting MySQL. SUCCESS!

service mysqld restart

mysql 登录

/usr/local/mysql/bin/mysql -u root -p

参考文档:

mysql修改root密码和设置权限

如何实现远程连接服务器MySQL

linux 安装MySql 5.7.20 操作步骤【亲测】

https://segmentfault.com/a/1190000012703513

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,527评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,314评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,535评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,006评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,961评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,220评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,664评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,351评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,481评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,397评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,443评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,123评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,713评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,801评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,010评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,494评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,075评论 2 341