ubuntu下 在线/离线安装mysql

Mysql安装有两种方式一种是在线联网安装,一种是离线安装

1、联网安装:

安装依赖包

 
  
  1. sudo apt-get install cmake
  2. sudo apt-get install libncurses5-dev

安装mysql

 
  
  1. sudo  apt-get install mysql-server
  2. sudo  apt-get isntall mysql-client
  3. sudo  apt-get install libmysqlclient-dev

查看是否安装成功:   

 
  
  1. sudo netstat -tap | grep mysql

通过上述命令检查之后,如果看到有mysql 的socket处于 listen 状态则表示安装成功。


 
  
  1. tcp        0      0 localhost:mysql         *:*                     LISTEN      1320/mysqld  

登陆mysql数据库        

 
  
  1. mysql -u root -p


查看mysql版本

 
  
  1. mysqladmin -u root -p version

停止mysql:

 
  
  1. sudo service mysql stop

启动mysql:

 
  
  1. sudo service mysql start

二、离线安装mysql

下载mysql安装包 

下载地址:http://dev.mysql.com/downloads/ 选择相关版本。

我这里下载的是mysql-5.5.50-linux2.6-x86_64.tar.gz 这个版本

1、解压并移动到/usr/local/mysql


 
  
  1. tar -zxvf mysql-5.5.50-linux2.6-x86_64.tar.gz
  2. sudo mv mysql-5.5.50-linux2.6-x86_64 /usr/local/mysql

2、添加用户组,赋权限

 
  
  1. groupadd mysql
  2. useradd -r -g mysql mysql
  3. sudo chown -R mysql:mysql mysql

接着进入mysql目录,修改mysql目录的拥有者,为mysql用户:

 
  
  1. cd /usr/local/mysql
  2. sudo chown -R mysql .
  3. sudo chgrp -R mysql .

3、安装数据库

 
  
  1. /usr/local/mysql$ sudo scripts/mysql_install_db --user=mysql
 
  
  1. fulong@FBI003:/usr/local/mysql$ sudo scripts/mysql_install_db --user=mysql
  2. Installing MySQL system tables...
  3. 160704 16:14:03 [Note] ./bin/mysqld (mysqld 5.5.50) starting as process 7089 ...
  4. OK
  5. Filling help tables...
  6. 160704 16:14:03 [Note] ./bin/mysqld (mysqld 5.5.50) starting as process 7095 ...
  7. OK
  8. To start mysqld at boot time you have to copy
  9. support-files/mysql.server to the right place for your system
  10. PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
  11. To do so, start the server, then issue the following commands:
  12. ./bin/mysqladmin -u root password 'new-password'
  13. ./bin/mysqladmin -u root -h FBI003 password 'new-password'
  14. Alternatively you can run:
  15. ./bin/mysql_secure_installation
  16. which will also give you the option of removing the test
  17. databases and anonymous user created by default.  This is
  18. strongly recommended for production servers.
  19. See the manual for more instructions.
  20. You can start the MySQL daemon with:
  21. cd . ; ./bin/mysqld_safe &
  22. You can test the MySQL daemon with mysql-test-run.pl
  23. cd ./mysql-test ; perl mysql-test-run.pl
  24. Please report any problems at http://bugs.mysql.com/

如果有如下报错

 
  
  1. Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

则需要安装有一个依赖包

 
  
  1. sudo apt-get install libaio-dev

4、启动mysql

 
  
  1. sudo ./support-files/mysql.server start


如下错误: 

 
  
  1. Starting MySQL
  2. .. * The server quit without updating PID file (/usr/local/mysql/data/FBI003.pid).

看下是否有启动的进程

 
  
  1. root@FBI003:/usr/local/mysql# ps -aux|grep mysql
  2. mysql     5145  0.0  1.1 426928 47160 ?        Ssl  15:44   0:02 /usr/sbin/mysqld
  3. root      8630  0.0  0.0  11072   680 pts/0    S+   16:50   0:00 grep --color=auto mysq

若有 kill掉,没有的话 考虑是否权限不对

设置mysql用户组权限

 
  
  1. chown -R mysql data

5、修改初始密码

 
  
  1. ./bin/mysqladmin -u root password 'fulong'

6、进入数据库

 
  
  1. root@FBI003:/usr/local/mysql/bin# ./mysql -u root -p

7、为了方便期间,可以配置环境变量,直接使用mysql命令

 
  
  1. sudo vim /etc/profile
  2. export MYSQL_HOME=/usr/local/mysql
  3. export PATH=$MYSQL_HOME/bin:$PATH
 
  
  1. source /etc/profile


8、添加mysql自启动

复制到init.d

 
  
  1. sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
  2. fulong@FBI003:/usr/local/mysql$

赋权限

 
  
  1. sudo chmod +x /etc/init.d/mysql

设置自启动

 
  
  1. sudo update-rc.d mysql defaults

移除自启动

 
  
  1. update-rc.d mysql remove

9、修改配置文件(可不做修改)

修改mysql最大连接数:

 
  
  1. cp support-files/my-medium.cnf ./my.cnfvim my.cnf,增加或修改max_connections=1024

关于my.cnf:mysql按照下列顺序搜索my.cnf:/etc,mysql安装目录,安装目录下的data。/etc下的是全局设置。





注意:

修改数据库远程模式

1、所有主机都能登录

 
  
  1. grant all privileges on *.* to root@"%" identified by "fulong" with grant option;

2、指定主机登录

 
  
  1. grant all privileges on *.* to root@"192.168.0.101" identified by "fulong" with grant option; flush privileges;

3、进mysql库查看host为%的数据是否添加:

 
  
  1. use mysql;
  2. select user,host from user;



查看日志状态

 
  
  1. show variables like 'log_%';




创建数据库:

1) 建库:create database hivedb;  

2) 建用户,赋权:grant all privileges on *.* to hivedb@"%" identified by "hivedb" with grant option;                           

3) 删除数据库:drop database hivedb;   

查看数据库: 

show databases;

查看当前字符集:

show variables like 'character%';


**************************************************************

启动mysql: 使用命令试试是否能连上mysql   

telnet 192.168.0.166 3306

若拒绝连接 编辑

sudo vim /etc/mysql/my.cnf

注释掉

#bind-address           = 127.0.0.1


即可










你可能感兴趣的:(linux)