Linux系统-MySQL数据库(上)

一、查看linux中有没有安装自带的mysql数据库, 如果安装了自带的mysql,则需要卸载

[root@www ~]# rpm -qa | grep mysql                # 查看系统中有没有安装过mysql
mysql-libs-5.1.71-1.el6.x86_64

[root@www ~]# yum -y remove mysql-libs            # 卸载 安装过的mysql-lib
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Remove Process
Resolving Dependencies
........此处卸载内容省略............
Removed:
  mysql-libs.x86_64 0:5.1.71-1.el6                                                                                       

Dependency Removed:
  cronie.x86_64 0:1.4.4-12.el6           cronie-anacron.x86_64 0:1.4.4-12.el6       crontabs.noarch 0:1.10-33.el6      
  postfix.x86_64 2:2.6.6-2.2.el6_1       sysstat.x86_64 0:9.0.4-22.el6             

Complete!
[root@www ~]# rpm -qa | grep mysql                # 再次查询看看有没有卸载干净
[root@www ~]# 

二、下载mysql软件包(这里用的是mysql 5.5 版本)

下载地址:

点击下载mysql5.5软件包


三、将软件包上传到 linux 系统的 /opt 目录下

[root@www opt]# ls
MySQL-5.5.48-1.linux2.6.x86_64.rpm-bundle.tar  rh
[root@www opt]#

四、解压 MySQL-5.5.48-1.linux2.6.x86_64.rpm-bundle.tar 文件

[root@www opt]# tar -xvf MySQL-5.5.48-1.linux2.6.x86_64.rpm-bundle.tar  # 解压 压缩文件
MySQL-client-5.5.48-1.linux2.6.x86_64.rpm
MySQL-shared-compat-5.5.48-1.linux2.6.x86_64.rpm
MySQL-test-5.5.48-1.linux2.6.x86_64.rpm
MySQL-server-5.5.48-1.linux2.6.x86_64.rpm
MySQL-embedded-5.5.48-1.linux2.6.x86_64.rpm
MySQL-devel-5.5.48-1.linux2.6.x86_64.rpm
MySQL-shared-5.5.48-1.linux2.6.x86_64.rpm

[root@www opt]# ls                                                      # 查看解压后的情况
MySQL-5.5.48-1.linux2.6.x86_64.rpm-bundle.tar  MySQL-shared-5.5.48-1.linux2.6.x86_64.rpm
MySQL-client-5.5.48-1.linux2.6.x86_64.rpm      MySQL-shared-compat-5.5.48-1.linux2.6.x86_64.rpm
MySQL-devel-5.5.48-1.linux2.6.x86_64.rpm       MySQL-test-5.5.48-1.linux2.6.x86_64.rpm
MySQL-embedded-5.5.48-1.linux2.6.x86_64.rpm    rh
MySQL-server-5.5.48-1.linux2.6.x86_64.rpm
[root@www opt]# 

五、在/opt 目录下安装 mysql的server端, 使用命令 yum -y install 包名

root@www opt]#  yum -y install MySQL-server-5.5.48-1.linux2.6.x86_64.rpm   #安装服务端
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
.............. 此处省略输出信息....................
Please report any problems at http://bugs.mysql.com/

  Verifying  : MySQL-server-5.5.48-1.linux2.6.x86_64                                                                 1/1 

Installed:
  MySQL-server.x86_64 0:5.5.48-1.linux2.6                                                                                

Complete!                                    # 安装成功
[root@www opt]# 

六、安装 mysql的client端

[root@www opt]# yum -y install MySQL-client-5.5.48-1.linux2.6.x86_64.rpm  # 安装 client端
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Setting up Install Process
Examining MySQL-client-5.5.48-1.linux2.6.x86_64.rpm: MySQL-client-5.5.48-1.linux2.6.x86_64

.........................此处省略中间输出信息.......................

Running Transaction
  Installing : MySQL-client-5.5.48-1.linux2.6.x86_64                                                                 1/1 
  Verifying  : MySQL-client-5.5.48-1.linux2.6.x86_64                                                                 1/1 

Installed:
  MySQL-client.x86_64 0:5.5.48-1.linux2.6                                                                                

Complete!                                                 # 安装完成   
[root@www opt]# 

七、检查 安装mysql后, 系统中是否添加了mysql的用户和组

[root@www opt]# grep mysql /etc/passwd                    
mysql:x:496:493:MySQL server:/var/lib/mysql:/bin/bash
[root@www opt]# grep mysql /etc/group
mysql:x:493:
[root@www opt]# 

八、检查mysql服务是否启动, 如果没启动,则开启mysql服务

[root@www opt]# service mysql status            # 通过service 服务的方式查看 mysql 状态
 ERROR! MySQL is not running
[root@www opt]# /etc/init.d/mysql status        # 通过脚本 方式查看 mysql 状态
 ERROR! MySQL is not running
[root@www opt]# ps -ef | grep mysql             # 通过查看进程的方式,来判断 mysql 是否开启
root       2274   2025  0 22:08 pts/0    00:00:00 grep mysql
[root@www opt]# 
[root@www opt]# service mysql start             # 启动 mysql 服务
Starting MySQL... SUCCESS! 
[root@www opt]# 
[root@www opt]# service mysql status            
 SUCCESS! MySQL running (2381)
[root@www opt]# /etc/init.d/mysql status
 SUCCESS! MySQL running (2381)
[root@www opt]# ps -ef | grep mysql
root       2288      1  0 22:09 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/www.pid
mysql      2381   2288  6 22:09 pts/0    00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/www.err --pid-file=/var/lib/mysql/www.pid
root       2422   2025  0 22:09 pts/0    00:00:00 grep mysql
[root@www opt]# 

九、设置 mysql 服务开机自启

[root@www opt]# chkconfig --list mysql        # 查看 mysql 服务开机自启情况
mysql          	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭
[root@www opt]# chkconfig mysql on            # 将 mysql 服务设置为开机自启
[root@www opt]# chkconfig --list mysql
mysql          	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
[root@www opt]# 

十、查看 mysql 服务监听端口

[root@www opt]# netstat -anpt | grep mysql   # 3306 端口为 mysql 服务的默认端口号
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2381/mysqld         
[root@www opt]# 

十一、刚装好的 mysql 服务在登录的时候是不需要密码的

[root@www opt]# mysql             # 刚装完 mysql 软件, 没有设置密码的时候, 不需要密码就能登录
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.48 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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 databases;            # 显示当前 数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> quit                       # quit 退出 
Bye
[root@www opt]# 

十二、 为 mysql 服务器 root管理员 设置登录密码

[root@www opt]# mysql                        # 使用 mysql 命令连接到本地 mysql 服务器
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.48 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> SET PASSWORD FOR 'root'@'localhost'=PASSWORD('abc123456');   # 设置 root 用户的密码
Query OK, 0 rows affected (0.00 sec)

mysql> quit                                                         # 退出 mysql 客户端
Bye
[root@www opt]# mysql                       # 再次使用 mysql 命令时, 提示要输入用户名和密码
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

十三、登录 以root 用户登录 mysql 服务器

[root@www opt]# mysql -u root  -pabc123456    # -u 指定登录用户名, -p指定密码,可以直接跟上密码. 相当于 先执行 mysql -u root  -p 然后再输入密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.5.48 MySQL Community Server (GPL)

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

十四、 字符集的设置

查看当前字符集:

[root@www opt]# mysql -u root -h 127.0.0.1 -pabc123456        # 登录 mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.5.48 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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 variables like '%char%';                         # 查看 mysql 字符集
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |    # 需要将 "latin1"改为 "uft-8"
| character_set_filesystem | binary                     |        
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> 

修改字符集:

[root@www opt]# cp /usr/share/mysql/my-huge.cnf /etc/my.cnf  # 1.将mysql出厂默认的配置文件拷贝一份
                    
[root@www opt]# vim /etc/my.cnf                              # 编辑 my.cnf 配置文件

[root@www opt]# cat /etc/my.cnf | grep -v ^#

[client]
port		= 3306
socket		= /var/lib/mysql/mysql.sock
default-character-set=utf8                 # 在 [client]下添加 default-character-set=utf8

[mysqld]                                   # 在[mysqld]下添加
character-set-server=utf8                  # character-set-server=utf8
character-set-client=utf8                  # character-set-client=utf8 
collation-server=utf8_general_ci           # collation-server=utf8_general_ci

port		= 3306
socket		= /var/lib/mysql/mysql.sock
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
thread_concurrency = 8


log-bin=mysql-bin

server-id	= 1



[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
default-character-set=utf8                  # 在[mysql]下添加default-character-set=utf8
no-auto-rehash

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
[root@www opt]#

再次查看修改后mysql系统中的字符集信息

[root@www opt]# service mysql restart    # 上一步修改了配置文件, 所以需要重启服务
Shutting down MySQL. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@www opt]# 
[root@www opt]# mysql -u root -h 127.0.0.1 -pabc123456    # 登录 mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.48-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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 variables like '%char%';            # 显示当前 mysql 数据库使用的字符集信息
+--------------------------+----------------------------+
| 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)

mysql> 

猜你喜欢

转载自blog.csdn.net/u010559460/article/details/87933127