mysql安装使用----1 安装和启动

1 安装 

Fedora16/17 Mysql 安装及配置


1.安装 Mysql Server

# yum install mysql mysql-server

2.开启 MySQL server 及开机启动 MySQL

# systemctl start mysqld.service  
# systemctl enable mysqld.service
ln -s '/usr/lib/systemd/system/mysqld.service' '/etc/systemd/system/multi-user.target.wants/mysqld.service'

或者使用:
# sudo chkconfig --levels 235 mysqld on
注意:正在将请求转发到“systemctl enable mysqld.service”。

3.MySQL Secure Installation

# /usr/bin/mysql_secure_installation 

通过操作将进行以下各项过程

  • Set (Change) root password
  • Remove anonymous users
  • Disallow root login remotely
  • Remove test database and access to it
  • Reload privilege tables output:
$ sudo /usr/bin/mysql_secure_installation 
[sudo] password for tiny: 




NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

或者对于本地测试环境直接可以使用以下命令来初始化root密码:

# mysqladmin -u root password [your_password_here]

4.本地连接 Mysql

$ mysql -u root -p
$ mysql -h localhost -u root -p

5.创建数据库,创建新用户及授予权限

## 创建数据库 flaskr ##
mysql> CREATE DATABASE flaskr;
 
## 创建用户 flaskr_user ##
mysql> CREATE USER 'flaskr_user'@'192.168.1.102' IDENTIFIED BY 'passwordxxx';
 
## 授予权限 ##
mysql> GRANT ALL ON flaskr.* TO 'flaskr_user'@'192.168.1.102';
 
##  FLUSH PRIVILEGES, reload the GRANT TABLES  ##
mysql> FLUSH PRIVILEGES;

转自:http://ouroboros.me/2012/06/30/mysql-install-and-configure-on-fedora1617/

出现问题:

出此是用mysql,因为root权限过高,所以新建一用户appadmin,权限仅为要用到的数据库。创建语句如下:grant select,insert,update,delete on test.* to appadmin@"%" identified by "password";其中@“%”是可以在任何地址登录。 
创建后到mysql.user下查看,有该用户。但是使用mysql -u appadmin -ppassword 登录,提示无法登录:ERROR 1045 (28000): Access denied for user 'appadmin'@'localhost' (using password: YES) 
百思不得其解,遂google,其中有人说到“mysql.user 表中有另外一些记录产生了作用,最有可能的就是已经有一条''@localhost记录,就是用户名是空,主机字段是localhost的记录。” 影响了。查看该表果然有。 
mysql> select host,user,password from mysql.user; 
+-----------+------------------+-------------------------------------------+ 
| host      | user             | password                                  | 
+-----------+------------------+-------------------------------------------+ 
| localhost | root             | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | 
| mza       | root             | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | 
| 127.0.0.1 | root             | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | 
| localhost |                  |                                           | 
| mza       |                  |                                           | 
| localhost | debian-sys-maint | *19DF6BF8310D46D681AE072AB73ECEC99C018C19 | 
| %         | appadmin         | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 | 
+-----------+------------------+-------------------------------------------+ 
7 rows in set (0.00 sec) 
但是删除那些为空(匿名)的用户后仍然无法登录。(可能是因为没有重启mysql)于是只好耐着性子看mysql参考手册。发现其中增加用户部分有这么一段话: 
其中两个账户有相同的用户名monty和密码some_pass。两个账户均为超级用户账户,具有完全的权限可以做任何事情。一个账户 ('monty'@'localhost')只用于从本机连接时。另一个账户('monty'@'%')可用于从其它主机连接。请注意monty的两个账户必须能从任何主机以monty连接。没有localhost账户,当monty从本机连接时,mysql_install_db创建的localhost的匿名用户账户将占先。结果是,monty将被视为匿名用户。原因是匿名用户账户的Host列值比'monty'@'%'账户更具体,这样在user表排序顺序中排在前面。 
这段话说的很清楚,因此执行 grant select,insert,update,delete on test.* to appadmin@"localhost" identified by "password"; 
退出后用appadmin登录,成功。




2 经实验可用的一些命令


可能是mysql版本不同,所以命令有小的区别。

增加权限:

GRANT ALL PRIVILEGES ON *.* TO algo@localhost IDENTIFIED BY 'ziyoren' WITH GRANT OPTION;


2 启动和关闭

基本方式

一、启动方式

1、使用 service 启动:service mysqld start

2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start

3、使用 safe_mysqld 启动:safe_mysqld&


二、停止

1、使用 service 启动:service mysqld stop

2、使用 mysqld 脚本启动:/etc/inint.d/mysqld stop

3、 mysqladmin shutdown

三、重启

1、使用 service 启动:service mysqld restart

2、使用 mysqld 脚本启动:/etc/inint.d/mysqld restart


四  某些情况

使用/etc/init.d/mysql  start/stop/restart

备注:查看mysql端口是否已经使用,使用netstat -anp 命令查看服务器端口使用情况。


2 启动多个实例


a) 配置文件。 例如 [mysqld_muti] 和 [mysqld1] 这个必须需要配置,需要多个实例,就增加mysqd的配置。


[mysqld_multi]
mysqld     = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user       = root

[mysqld1]
socket     = /tmp/mysql1.sock
port       = 3306
pid-file   = /data/mysqld1/1.pid
datadir    = /data/mysqld1/
log        = /data/mysqld1/1.log
user       = mysql

[mysqld2]
socket     = /tmp/mysql2.sock
port       = 3307
pid-file   = /data/mysqld2/2.pid
datadir    = /data/mysqld2/
log        = /data/mysqld2/2.log
user       = mysql

b) 因为mysql服务启动默认会有mysql和test这个库,而mysql这个库是mysql启动时需要用到的。所以我们把
mysql库的数据文件拷贝至我们想要启动的实例的数据库路径。并赋予mysql可读写的权限。 (这一步不做的话,启动实例会报找不到mysql.host ,等找不到mysql数据库的错误)

c) 配置好后,mysqld_multi --config-file=/etc/my.cnf.test start 1,2

这样就启动了。 

d) ps -ef|grep mysqld  看是否mysql实例已经启动

e) 每个实例的日志分别在各自的datadir下。 请自行查找 *.err 的文件,并查看什么启动错误,好定位问题。

f) 停止实例 mysqld_multi --config-file=/etc/my.cnf.test stop 1,2


启动完毕后,可以访问各实例了。  mysql -P 3307/3306 -h 127.0.0.1

在crazyhacking的机器上,命令为:

mysqld_multi --defaults-file=/etc/my.cnf  start 3306

具体选项使用mysqld_multi  --help得到



理论知识

 mysqld --verbose --help可以得到使用说明和mysql的默认配置信息,如各种默认目录等:


1   默认配置文件的访问顺序
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf



原文地址:https://www.cnblogs.com/catkins/p/5270619.html