Tuesday, September 29, 2015

Recover MySQL root password

Step # 1 : Stop the MySQL service:
# /etc/init.d/mysql stop
Output:
Stopping MySQL database server: mysqld.
Step # 2: Start the MySQL server w/o password:
# mysqld_safe --skip-grant-tables &
Output:
[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started
Step # 3: Connect to the MySQL server using the MySQL client:
# mysql -u root
Output:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>
Step # 4: Set a new MySQL root user password:
mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit
Step # 5: Stop the MySQL server:
# /etc/init.d/mysql stop
Output:
Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended

[1]+  Done                    mysqld_safe --skip-grant-tables
Start the MySQL server and test it:
# /etc/init.d/mysql start
# mysql -u root -p

Source: https://www.howtoforge.com/setting-changing-resetting-mysql-root-passwords

Friday, September 11, 2015

mongo replica with authentication

- Have not to create anything before
connect to mongod using the localhost exception

>use admin
>db.createUser(
    {
      user: "superuser",
      pwd: "12345678",
      roles: [ {role: "userAdminAnyDatabase", db: "admin"}, "root" ]
    }
)

=========To create use in database===========
>use another_database
>db.createUser({user: "name", pwd: "password", roles: ['read', 'readWrite', 'dbAmin', 'dbOwner', 'userAdmin', ... http://docs.mongodb.org/v2.6/reference/built-in-roles/ ]})
====================End======================
- Close mongod
 
- Create keyFile
#openssl rand -base64 741 > mongodb-keyfile
#chmod 600 mongodb-keyfile
 
- Copy key file to each member of replica set
 
- Start mongod: mongod --auth --setParameter enableLocalhostAuthBypass=0 --setParameter enableTestCommands=0 --dbpath /your/path --replSet rs0 --smallfiles --oplogSize 128 --keyFile /path/to/mongodb-keyfile 
  Or edit in file config

dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongod.log
logappend=true
bind_ip = 127.0.0.1
auth = true
smallfiles = true
oplogSize = 128
keyFile = /path/to/mongodb-keyfile
replSet = rs0
setParameter=enableLocalhostAuthBypass=0
setParameter=enableTestCommands=0

- Connect to mongod
#./mongo
>use admin
>db.auth('superuser', '12345678');
>rs.initiate()
- Add secondaries 
>rs.add('host:port')

================================================================================
db.getCollectionNames().forEach(function(c) { if (c.indexOf("system.") == -1) db[c].drop();